英文:
Can I pass a parameter from URL to Livewire Laravel?
问题
I understand that you want a translation of the provided code and content. Here's the translated content without the code:
我是Laravel Livewire的新手。我想要能够在Laravel Livewire的模板文件中使用从URL传递的参数。我已经能够从路由中访问该参数,在控制器中执行逻辑并将其传递到组件模板视图文件,但我有点困惑如何在Livewire视图文件中传递或访问传递给组件视图文件的对象,而这些对象用于显示逻辑。
在这种情况下,涉及到的主要文件和代码如下:
routes/web.php
Route::get('/productcategory/{category}', [App\Http\Controllers\CategoryController::class, 'productCategory'])->name('productcategory');
App/HTTP/controller/CategoryController.php
public function productCategory($category)
{
$cat = str_replace("+", " ", $category);
$categories = Categories::all();
$products = Product::where('category', $cat)->paginate(10);
return view('components.productcategories', compact('categories', 'products'));
}
resources/views/component/productcategories.blade.php
@extends('layouts.newapp')
@section('title','Product Category')
@section('content')
<div>
@include('inc.header')
@livewire('category')
@include('inc.footer')
</div>
@endsection
App/HTTP/livewire/Productcategory.php
public function render()
{
return view('livewire.category');
}
resources/views/livewire/category.blade.php
@if (!empty($products))
@foreach ($products as $item)
<p>{{ $item->name}}</p>
@endforeach
@endif
请问您需要进一步的帮助吗?
英文:
I am new to Laravel Livewire. I want to be able to use the parameter passed from the URL in the Laravel livewire blade file. I have been able to access the parameter in the controller from the route, performed the logic and pass it to the component blade view file, but I am a bit confused how to pass or access the object passed to the component view file in the livewire view file where the display logic is located.
I have the major files and codes involved in this scenario:
routes/web.php
Route::get('/productcategory/{category}', [App\Http\Controllers\CategoryController::class, 'productCategory'])->name('productcategory');
App/HTTP/controller/CategoryController.php
public function productCategory($category)
{
$cat = str_replace("+"," ",$category);
$categories = Categories::all();
$products= Product::where('category', $cat)->paginate(10);
return view('components.productcategories', compact('categories','products'));
}
resources/view/component/productcategories.blade.php
@extends('layouts.newapp')
@section('title','Product Category')
@section('content')
<div>
@include('inc.header')
@livewire('category')
@include('inc.footer')
</div>
@endsection
App/HTTP/livewire/Productcategory.php
public function render()
{
return view('livewire.category');
}
resources/view/livewire/category.blade.php
@if (!empty($products))
@foreach ($products as $item)
<p>{{ $item->name}}</p>
@endforeach
@endif
Please I need help with this.
答案1
得分: 2
自从您使用路由模型绑定后,它将会自动提供给mount
函数:
public $category;
public function mount(Category $category)
{
$this->category = $category;
}
在PHP 7.4+ 中,您可以跳过mount()
,使其更加简单;只需声明参数并匹配路由文件中的类和变量名:
class YourLivewireComponent extends Component
{
public Category $category;
}
如果您只想获取一个随机的GET参数,可以注入Request类:
public $foo;
public function mount(Request $request)
{
$this->foo = $request->get('foo');
}
(您可以以这种方式注入任何依赖项,不仅仅是Request。)
英文:
Since you're using route model binding, it'll be automatically provided to the mount
function:
public $category;
public function mount(Category $category)
{
$this->category = $category;
}
In PHP 7.4+, you can skip the mount()
so it's even simpler; just declare the parameter and match the class and variable name from your routes file.
class YourLivewireComponent extends Component
{
public Category $category;
}
If you just wanted to get a random GET parameter, inject the Request class:
public $foo;
public function mount(Request $request)
{
$this->foo = $request->get('foo');
}
(You can inject any dependency this way, not just Request.)
答案2
得分: 0
你可以像这样在@livewire()
指令中传递它,只需确保$product
已声明到您的livewire组件类中:
Blade组件
@livewire('category', [
'products' => $products,
])
Livewire类
class Productcategory
{
public $products;
public function render()
{
return view('livewire.category');
}
}
英文:
You can pass it in the @livewire()
directive like just make sure that the $product
is declared to your livewire component class:
Blade Component
@livewire('category', [
'products' => $products,
])
Livewire Class
class Productcategory
{
public $products
public function render()
{
return view('livewire.category');
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论