Reputation: 27
I believe I am close but struggling on how to pass a given ID from my show method into the associated showdata one so that datatables can manifest with the associated data for just that ID.
The code works when I hardcode an ID or just remove the where statement altogether. My objective is to have the datatable manifest associated items for just a given collection.
Routes:
Route::resource('collections', 'CollectionsController');
Route::get('collectionsdata/{id}', 'CollectionsController@showdata');
Show.blade.php:
var id = {{ $collection->id }}
$(function () {...
$('#item-table').DataTable({
//processing: true,
serverSide: true,
ajax: "/collectionsdata/"+ id,
columns: [
//{data: 'id', name: 'id'},
{data: 'id', name: 'id'},
{data: 'collection_id', name: 'collection_id'},
{data: 'type_id', name: 'type_id'},
{data: 'collection.title', name: 'collection.title'},
{data: 'type.name', name: 'type.name'},
{data: 'updated_at', name: 'updated_at'},
]
});
Collections Controller:
public function show(Collection $collection)
{
$collection->load('items.type')->get();
//dd($collection);
return view ('collections.show', compact('collection'));
}
public function showdata()
{
$data = Item::with('collection', 'type')->where('collection_id', $id);
return Datatables::of($data)->make(true);
}
SHOW itself function well, var id works well in the blade - I think I am just missing something in the controller to take in the id and ultimately create the desired query on $data for return to the datatable.
Upvotes: 1
Views: 2081
Reputation: 69
You only have to have one parameter in showdata that is one for id you are passing from URL
public function showdata($id)
{
$data = Item::with('collection', 'type')->where('collection_id', $id);
return Datatables::of($data)->make(true);
}
Upvotes: 1
Reputation: 2540
Yes. You are missing the Request $request in your show method to retrieve the $id from the route.
use Illuminate\Http\Request;
public function showdata(Request $request)
{
$collection_id = $request->id;
$data = Item::with('collection', 'type')->where('collection_id', $id);
return Datatables::of($data)->make(true);
}
You can also retrieve id directly from the controller instead of using $request.
public function showdata($id)
{
$data = Item::with('collection', 'type')->where('collection_id', $id);
return Datatables::of($data)->make(true);
}
Upvotes: 1