Laravel-datatables: How to pass variables into raw column view?

Created on 6 Feb 2020  路  2Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

Hi, I created raw columns, and I need to pass routes for action buttons. How can I do this?

Code snippet of problem


namespace App\Datatables;

use App\Models\Track;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;

class TracksDataTable extends DataTable
{
    protected $route = "/admin/routes/edit"
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            // here I need to pass route ($this->route ) for editing or deleting a track
            ->addColumn('action', 'datatables.actions')
            ->rawColumns(['action']);
    }

    public function query(Track $model)
    {
        return $model->newQuery();
    }

// ... other code, that probably not related to that question

MY VIEW FILE ('datatables.actions')

<a href="{{route($route.'.edit', $id)}}">edit</a>
<a href="{{route($route.'.destroy', $id)}}">delete</a>
question

Most helpful comment

Use add column with closure: https://yajrabox.com/docs/laravel-datatables/master/add-column#closure

Route::get('user-data', function() {
    $model = App\User::query();

    return DataTables::eloquent($model)
                ->addColumn('intro', function(User $user) {
                    $route = $this->route;
                    return view('datatables.action', compact('route', 'user'));
                })
                ->toJson();
});

All 2 comments

Use add column with closure: https://yajrabox.com/docs/laravel-datatables/master/add-column#closure

Route::get('user-data', function() {
    $model = App\User::query();

    return DataTables::eloquent($model)
                ->addColumn('intro', function(User $user) {
                    $route = $this->route;
                    return view('datatables.action', compact('route', 'user'));
                })
                ->toJson();
});

thank you very much it s what i was searching for

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jgatringer picture jgatringer  路  3Comments

jackrsantana picture jackrsantana  路  3Comments

techguydev picture techguydev  路  3Comments

Mopster picture Mopster  路  3Comments

alejandri picture alejandri  路  3Comments