1

I am new to Laravel DataTables and i am trying to insert my controller in the datatables but i am getting an exception.

How to get this done?

PS: When I use the normal route like user/items/'$users->id'/control, it works fine but I want to use the the route like UserController@edit, $user->id

public function getPost()
{
    $users= User::where('id', Auth::user()->id);
    return Datatables::of($users)->addColumn('action', function ($user) {
        return '<a href="{ action('UserController@edit', $user->id)}" title="edit" ><i class="fa fa-edit"></i></a>
                &nbsp;&nbsp;';

    })->make(true);     

}
9
  • what datatable? where's your datatable? Commented Dec 15, 2017 at 8:27
  • @dexterb, i don't think i need to post my datatable here. I want to know if it is possible to do what i am asking in the question Commented Dec 15, 2017 at 8:28
  • you should specify what datatable package you are using. Commented Dec 15, 2017 at 8:31
  • @dexterb i am using Yajra package Commented Dec 15, 2017 at 8:32
  • 1
    try like this return '<a href="'.action('UserController@edit', ['id' => $user->id]).'" title="edit" ><i class="fa fa-edit"></i></a>'; Commented Dec 15, 2017 at 8:37

3 Answers 3

2

Some mentions to your code:

// this will get you only one user
$users = User::where('id', Auth::user()->id);

// therefore it does not make sense to show one user in a table, does it ;-)
return Datatables::of($users)->make(true);     

To your question:

You need two actions: One to render the view and one to fetch the data for the datatables via Ajax (if you use ServerSide rendering)

Assuming you want to show a list of users:

Routes

Route::get('users', 'UserController@index');
Route::get('users-dt', 'UserController@indexDataTables')->name('users:dt');

Controller

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

public function indexDataTables()
{
    $users = User::query();

    return DataTables::eloquent($users)->toJson();
}

user/index.blade.php

<script>
    $(function() {
        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! route('users:dt') !!}',
            columns: [
                { data: 'name', name: 'name'},
            ],
        });
    })
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You actually forgot to specify the parameter name on the second argument of action helper.

action('UserController@edit', ['user' => $user->id]);

// Assuming that UserController@edit looks like this:

public function edit(User $user) { ... }

Comments

0
href="'.action('UserController@edit', $user->id).'"

Just remove curly braces. with ('..')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.