简体   繁体   中英

How can i add a route link to data table column in laravel?

I have a system that shows the data of all available users of the system with search function. I have done that by using yajra data table package. What I want is if anyone clicks on a row of user that will redirect to that users profile. Here is the controller code

public function getUsers(){
    return DataTables::of(User::query()->with('profile'))->make(true);
}

public function index(){
    return view('alumni.list');
}

This is the blade code for data table

$(document).ready( function () {
    $('#alumniTable').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('alumni-members') }}",
        columns: [
            { data: 'id' , name: 'id'},
            { data: 'name' , name: 'name'},
            { data: 'email' , name: 'email'},
            { data: 'student_id' , name: 'student_id'},
            { data: 'created_at' , name: 'created_at'},

        ]

    });
});

route code:

Route::get('/alumni-members', 'AlumniController@index');
Route::get('users','AlumniController@getUsers')->name('alumni-members');

if you need to add route link column than add column in datatable

return Datatables::of(User::query()->with('profile'))
->addColumn('namelink', function ($user) {
    return '<a href="' . route('users.show', $user->id) .'">'.$user->name.'</a>'; 
})
->rawColumns(['namelink'])
->make(true);

and also add column in columns array:

$(document).ready( function () {
$('#alumniTable').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{ route('alumni-members') }}",
    columns: [
        { data: 'id' , name: 'id'},
        { data: 'name' , name: 'name'},
        { data: 'email' , name: 'email'},
        { data: 'student_id' , name: 'student_id'},
        { data: 'created_at' , name: 'created_at'},
        {data: 'namelink', name: 'namelink', orderable: false, searchable: 
        false},

    ]

   });
});

this worked but with a slight change.

return datatables()->of($d) ->addColumn('ab_id',function($d){ return "<a href='". url ('view-detail',$d-

ab_id)."'>".$d->ab_id."";
}) ->rawColumns(['ab_id']) ->make(true);

instead of the route, I called URL function and it worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM