yajra/laravel-datatables-html

How do you pass parameters into a view when using the addColumn() API?

RyanPriceDotCa opened this issue · 1 comments

Summary of problem or feature request

How do you pass parameters into a view when using the addColumn() API?

Code snippet of problem

My view looks something like (simplified for readability):

<a href="{{route('user.edit', $user)}}" target="_blank"><button type="button" class="btn btn-primary">Edit</button></a>

My function looks like:

    public function datatable()
    {
        return DataTables::eloquent(User::query())
                ->addColumn('action', 'user.partials.actions') // <-- Need to pass the $user to this view
                ->toJson();
    }

Docs seem to suggest something gets passed along implicitly?

https://yajrabox.com/docs/laravel-datatables/master/add-column#view

You may use something like this (your $user is passed as $model):

{{ route('user.edit', $model) }}

or you may change your $user to $id, since each attributes in your User model passed to that view.

{{ route('user.edit', $id) }}

or you can use a callback that returns a rendered view:

->addColumn('action' , function (User $user) {
    return view('user.partials.actions', compact('user'))->render();
})