laravel 嵌套路由的 livewire 问题

laravel livewire issue with nested routes

所以我有一个嵌套路由 URL 和“/study/2/sites”一样,在我的 livewire 组件中,我试图捕获研究 ID,以便我可以在我的 [=37] 中使用它=]查询。但它似乎没有用..代码在下面

在我的站点资源控制器中我有 .

public function index(Study $study)
    {
       return view('sites', compact('study'));
    }

然后在 sites.blade.php 这是我拥有的视图组件

@extends('layouts.app')


@section('content')
    @livewire('sites-table', ['$study',$study])
@endsection

然后在我的 livewire 组件站点-table.blade.php 我有

    <table
        class="table table-vcenter table-mobile-md card-table">
        <thead>
        <tr>
            <th>Site Number</th>
            <th>Site Name</th>
            <th>Country</th>
            <th>Investigator Name</th>
            <th>Investigator Email</th>
            <th class=""></th>
        </tr>
        </thead>
        <tbody>

        @forelse($sites as $site)
        <tr>
            <td data-label="Name" >
                <div class="d-flex py-1 align-items-center">
                    {{ $site->site_number }}
                </div>
            </td>
            <td data-label="Title" >
                {{$site->site_name}}
            </td>
            <td class="text-muted" data-label="Role" >
                {{ $site->country }}
            </td>
            <td>
                {{ $site->investigator_name }}
            </td>
            <td>
                {{ $site->investigator_email }}

            </td>
            <td></td>
            <td>
                <div class="btn-list flex-nowrap">
                    <a href="#" class="btn">
                        Edit
                    </a>
                    <div class="dropdown">
                        <button class="btn dropdown-toggle align-text-top" data-bs-toggle="dropdown">
                            Actions
                        </button>
                        <div class="dropdown-menu dropdown-menu-end">
                            <a class="dropdown-item" href="#">
                                Action
                            </a>
                            <a class="dropdown-item" href="#">
                                Another action
                            </a>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        @empty
            <tr class="text-center">No available Result</tr>
        @endforelse

        </tbody>


    </table>

        <div class="card w-100">
        <div style="float: right" class="pt-2">
            {{ $sites->links() }}
        </div>
        </div>

</div>

最后,在我的 livewire SitesTable 控制器中,我有这个

public $study;

   
public function mount(Study $study)

{

    $this->study = $study;

}
   
public function render()
{
        return view('livewire.sites-table',['sites'=>Sites::where('study_id',$this->study)->paginate(9),]);
}

知道我做错了什么吗??我需要获取当前研究的 ID,以便我可以使用它并找到该研究下可用的所有站点

已编辑:::

所以我最终这样做了

public $study;


public function mount()

{

    $study = \Route::current()->parameter('study');
    $this->study = $study;
}

public function render()
{
        return view('livewire.sites-table',['sites'=>Sites::where('study_id',$this->study)->where(function ($query){
            $query->where('site_number','LIKE','%'.$this->searchData.'%')->orWhere('site_name','LIKE','%'.$this->searchData.'%');
        })->paginate(9),]);

}

这行得通,但我相信一定有更好的方法来实现这一目标

这样试试:

你的view

@livewire('sites-table', ['study' => $study]);

您的 Livewire 组件:

class SitesTable extends Component
{
    public $study;
    
    // rest of the component goes here.
}