你可以将参数从URL传递给Livewire Laravel吗?

huangapple go评论71阅读模式
英文:

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(&#39;/productcategory/{category}&#39;, [App\Http\Controllers\CategoryController::class, &#39;productCategory&#39;])-&gt;name(&#39;productcategory&#39;);

App/HTTP/controller/CategoryController.php

public function productCategory($category)
    {
        $cat = str_replace(&quot;+&quot;,&quot; &quot;,$category);
        $categories = Categories::all();
        $products= Product::where(&#39;category&#39;, $cat)-&gt;paginate(10);
        return view(&#39;components.productcategories&#39;, compact(&#39;categories&#39;,&#39;products&#39;));
    }

resources/view/component/productcategories.blade.php

@extends(&#39;layouts.newapp&#39;)

@section(&#39;title&#39;,&#39;Product Category&#39;)
@section(&#39;content&#39;)
&lt;div&gt;
    @include(&#39;inc.header&#39;)

    @livewire(&#39;category&#39;)
   
    @include(&#39;inc.footer&#39;)
&lt;/div&gt;
@endsection

App/HTTP/livewire/Productcategory.php

public function render()
    {
        return view(&#39;livewire.category&#39;);
    }

resources/view/livewire/category.blade.php

@if (!empty($products))
  @foreach ($products as $item)
     &lt;p&gt;{{ $item-&gt;name}}&lt;/p&gt;
  @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-&gt;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-&gt;foo = $request-&gt;get(&#39;foo&#39;);
}

(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(&#39;category&#39;, [
    &#39;products&#39; =&gt; $products,
])

Livewire Class

class Productcategory
{
    public $products

    public function render()
    {
        return view(&#39;livewire.category&#39;);
    }
}

huangapple
  • 本文由 发表于 2023年5月21日 21:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定