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);
}
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>