So I have a nested route URL as so "/study/2/sites" and in my livewire component, I'm trying to catch the study id so I can use it in my eloquent query. but it doesn't seem to be working.. the code is below
in my sites resource controller I have .
public function index(Study $study)
{
return view('sites', compact('study'));
}
then in sites.blade.php which is the view component i have
@extends('layouts.app')
@section('content')
@livewire('sites-table', ['$study',$study])
@endsection
then in my livewire component site-table.blade.php i have
<table
>
<thead>
<tr>
<th>Site Number</th>
<th>Site Name</th>
<th>Country</th>
<th>Investigator Name</th>
<th>Investigator Email</th>
<th ></th>
</tr>
</thead>
<tbody>
@forelse($sites as $site)
<tr>
<td data-label="Name" >
<div >
{{ $site->site_number }}
</div>
</td>
<td data-label="Title" >
{{$site->site_name}}
</td>
<td data-label="Role" >
{{ $site->country }}
</td>
<td>
{{ $site->investigator_name }}
</td>
<td>
{{ $site->investigator_email }}
</td>
<td></td>
<td>
<div >
<a href="#" >
Edit
</a>
<div >
<button data-bs-toggle="dropdown">
Actions
</button>
<div >
<a href="#">
Action
</a>
<a href="#">
Another action
</a>
</div>
</div>
</div>
</td>
</tr>
@empty
<tr >No available Result</tr>
@endforelse
</tbody>
</table>
<div >
<div style="float: right" >
{{ $sites->links() }}
</div>
</div>
</div>
lastly, in my livewire SitesTable controller, i have this
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),]);
}
Any idea on what I'm doing wrong?? I need to catch the id of the current study so i can use it and find all the sites that are available under the study
Edited:::
so I ended up doing this
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),]);
}
which works but i'm sure there must be a better way to achieve this
CodePudding user response:
Try it like this:
Your view
:
@livewire('sites-table', ['study' => $study]);
Your Livewire Component:
class SitesTable extends Component
{
public $study;
// rest of the component goes here.
}