Laravel-datatables: How to create a default column for all views?

Created on 21 Feb 2017  ·  3Comments  ·  Source: yajra/laravel-datatables

I would like to know how I can include a column with the include and edit buttons for all screens, without duplicating the code for each view?

I was able to create the column with the buttons, but the way I did it, I would have to repeat the code for each view. This would increase maintenance and possibly propagate bugs.

if ($request->ajax()) {
            $cidades = Cidade::with('estado')->get();

            return Datatables::of($cidades)
                ->addColumn('action', function ($cidades) {
                    $editar  =
                        '<a href="' . route($this->dados['rota'] . '.edit', $cidades->id) . '" class="ui icon button yellow">' .
                        '   <i class="icon write"></i>' .
                        '</a>';
                    $deletar = '<form action="' . route($this->dados['rota'] . '.destroy', $cidades->id) . '" method="post" style="display: inline">' .
                        csrf_field() .
                        method_field('DELETE') .
                        '   <button type="submit" class="ui icon button red">' .
                        '       <i class="icon trash"></i>' .
                        '</button >' .
                        '</form >';

                    return $editar . $deletar;
                })
                ->make(true);
        }

System details

  • Operating System Windows 10
  • PHP Version 7.1
  • Laravel Version 5.4
  • Laravel-Datatables Version 1.10.13
question

All 3 comments

Extract your buttons code to a partial view then re-use it.

```php
->addColumn('action', function ($cidades) {
return view('path.to.buttons', compact('cidaded')->render();
})

I was able to do what you suggested. I moved the buttons to a separate view. However, I'm now not sure how I can send the ids to the buttons so that they are synchronized with their proper records when rendered by the view.

<a href="{{HOW DO I GET THE ID FROM HERE}}/edit" class="ui icon button yellow">
    <i class="icon write"></i>
</a>

<form action="{{HOW DO I GET THE ID FROM HERE}}" method="post" style="display: inline">
    {{ csrf_field() }}
    {{ Form::hidden("_method", "DELETE") }}

    <button type="submit" class="ui icon button red">
        <i class="icon trash"></i>
    </button>
</form>

You can use a generic variable name like model:

->addColumn('action', function ($model) {
    return view('path.to.buttons', compact('model')->render();
})
<form action="{{$model->id}}" method="post" style="display: inline">
    {{ csrf_field() }}
    {{ Form::hidden("_method", "DELETE") }}

    <button type="submit" class="ui icon button red">
        <i class="icon trash"></i>
    </button>
</form>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nasirkhan picture nasirkhan  ·  3Comments

macnux picture macnux  ·  3Comments

NidhiDesai11 picture NidhiDesai11  ·  3Comments

SGarridoDev picture SGarridoDev  ·  3Comments

techguydev picture techguydev  ·  3Comments