Chumper / Datatable

This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
https://github.com/Chumper/Datatable
388 stars 154 forks source link

how to pas an id from view to controllers? #226

Open alexcatrin opened 9 years ago

alexcatrin commented 9 years ago

hello,

to generate the datatable in the view I do

     {{ $pic_table->render() }} 

//in route i have

Route::get('api/hotelPic/{id}',
                    array('as'=>'api.hotelPic',
                             'uses'=>'HotelController@getHotelPicTable'));

//hotel controler

       public function show($id)
   {
    //
    $hotel              =Hotel::find($id);
            $countries          =Country::All();
            $regions            =Region::All();
    $facilities         =Facility::where('type','=','hotel')->get();
    $facilities_room    =Facility::where('type','=','room')->get();
            $rooms              =Room::where('hotel_id','=',$id)->get();
            $pictures           =Picture::where('hotel_id','=',$id)->get();

         $pic_table=Datatable::table()
        ->addColumn('Thumbnail','Title','Active','Actions')
        ->setUrl(route('api.hotelPic'))
        ->setOptions(array(
            'dom'=>'<"hotelPic-datatable"<"alex-table "t><"alex-filter"f><"alex-length"l><"alex-information"i><"alex-pagination"p>>',

        ));

    return View::make('smart/hotels.view',compact('hotel'))
                    ->with(array('facilities'       =>$facilities,
                                 'facilities_room'  =>$facilities_room,
                                 'rooms'            =>$rooms,
                                 'countries'        =>$countries,
                                 'regions'          =>$regions,
                                 'pictures'         =>$pictures,
                                 'pic_table'        =>$pic_table
                        ));

}

the problem is that i don't know how to get the hotel_id from the view to the method below

public function getHotelPicTable($hotel_id){
  $pictures=Picture::where('hotel_id','=',$id);
}

help is greatly appreciated. thanks in advance

Chumper commented 9 years ago

Honestly i do not understand your problem, just access the id in the controller method.

alexcatrin commented 9 years ago

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.

Chumper commented 9 years ago

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.

alexcatrin commented 9 years ago

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?

alexcatrin commented 9 years ago

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?

johnhatch commented 9 years ago

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.'">';
})
alexcatrin commented 9 years ago

thank you for your prompt responses. grate add on, keep up the great job