Open alexcatrin opened 9 years ago
Honestly i do not understand your problem, just access the id in the controller method.
Ok ... But how do you send the id from view to controller when all you do in view is $table->render(). With out any parameter.... On Jan 20, 2015 6:26 PM, "Nils Plaschke" notifications@github.com wrote:
Honestly i do not understand your problem, just access the id in the controller method.
— Reply to this email directly or view it on GitHub https://github.com/Chumper/Datatable/issues/226#issuecomment-70683112.
Datatable is doing an ajax request to the url you specified to get all the needed data for displaying the table.
We are not sending any id to any controller, we just show the table with a view and datatable will do the rest.
so to better understand and explain my situation: i 'm building an application with hotels which have a relationship of many photos. in my show page of the hotel i try to display a data table with all the photos that the hotel have. so my page url is : hotels/1
in this view i'm doing a {{ $pic_table->render() }}
this leads to the getHotelPicTable()
method where i should do a selection of all the photos from this hotel.
public function getHotelPicTable(){
$pictures=Picture::where('hotel_id','=',$id)->get();
}
with this selection is my problem, how should i write it to return only the picture of the current hotel?
i have found a workaround:
in the show($id)
method of the hotelController
public function show($id){
$pic_table=Datatable::table()
->addColumn('Thumbnail','Title','Active','Actions')
->setUrl(route('api.hotelPic',$id)) //I added the $id
->setOptions(array(
'dom'=>'<"hotelPic-datatable"<"alex-table "t><"alex-filter"f><"alex-length"l><"alex-information"i><"alex-pagination"p>>'));
the route modified accordingly
Route::get('api/hotelPic/{id}',array(
'as'=>'api.hotelPic',
'uses'=>'HotelController@getHotelPicTable'));
an now the getHotelPicTable method like this:
public function getHotelPicTable($id){
$pictures=Picture::where('hotel_id','=',$id)->get();
return Datatable::collection($pictures)
->showColumns('file_path')
->make()
}
now another question is there a way to concatenate tow fields in one cell?
To concatenate multiple fields you will want to define a custom column with a function. Embedding an image as an example:
->addColumn('col_name', function($picture){
return '<img src="'.$picture->file_path.'" alt="'.$picture->title.'">';
})
thank you for your prompt responses. grate add on, keep up the great job
hello,
to generate the datatable in the view I do
//in route i have
//hotel controler
the problem is that i don't know how to get the hotel_id from the view to the method below
help is greatly appreciated. thanks in advance