Create template for routes in Laravel with resource

0

I would like to know how to create a template on the blade containing the generic actions (view, edit, delete) for all the routes of my application, for example in the actions column I have the code:

<td>
  <!-- botão Exibir -->
    <a href="{{ route('admin.cores.show',[$cor->id]) }}" class="@lang('global.button.fields.exibir')">@lang('global.app_show')
    </a>
  <!-- botão Editar -->
    <a href="{{ route('admin.cores.edit',[$cor->id]) }}" class="@lang('global.button.fields.editar')">@lang('global.app_edit')
    </a>
   <!-- botão Apagar -->
    {!! Form::open(array(
        'style' => 'display: inline-block;',
        'method' => 'DELETE',
        'onsubmit' => "return confirm('".trans("global.app_are_you_sure")."');",
        'route' => ['admin.cores.destroy', $cor->id])) !!}
    {!! Form::submit(trans('global.app_delete'), array('class' => trans('global.button.fields.apagar'))) !!}
    {!! Form::close() !!}
</td>

The route of the view:

Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {

    //Usuários e Permissões
    Route::resource('users', 'Admin\UsersController');
    Route::post('users_mass_destroy', ['uses' => 'Admin\UsersController@massDestroy', 'as' => 'users.mass_destroy']);
});

My idea would be to create a single button template and avoid repeating code and be able to control the policies in just one file.

For example, create an acoesTemplate.blade.php file with the following lines:

  <a href="{{ route($routeKey.'.show', $row->id) }}" class="@lang('global.button.fields.exibir')">@lang('global.app_show')</a>
  <a href="{{ route($routeKey.'.edit', $row->id) }}" class="@lang('global.button.fields.editar')">@lang('global.app_edit')</a>
    {!! Form::open(array(
        'style' => 'display: inline-block;',
        'method' => 'DELETE',
        'onsubmit' => "return confirm('".trans("global.app_are_you_sure")."');",
        'route' => [$routeKey.'.destroy', $row->id])) !!}
    {!! Form::submit(trans('global.app_delete'), array('class' => 'btn btn-xs btn-danger')) !!}
    {!! Form::close() !!}

And then just call this file in the display columns, but I do not know how to implement the variables in the routes.

    
asked by anonymous 15.06.2018 / 22:43

2 answers

1

I imagine it's more or less what you want:

<!--Esse é um exemplo do blade dos botões-->
<a href="#"  id='showBTN' class="btn btn-success openmodal" 
title="Visualizar">
<i class="fa fa-eye"></i>
</a>

<a href="{{route( $rota.'.edit' , $id)}}" class="btn btn-primary"      title="Editar">
<i class="fa fa-edit"></i>
</a>

A blade imports the above file by passing the id (or whatever information you want):

 @include('cadastro.template.acoesBTN', array('id'=>$p->id))

And finally the method of your controller that treats the route referring to that view

public function index(Produtor $produtor)
{       
    $produtores = $produtor->all();
    $rota = 'cadastro.produtor';
    return view('cadastro.view.produtorView',
        compact('produtores', 'rota'));
}
    
20.06.2018 / 16:25
1

You can create a botao.blade.php

and then import into your code, you will have to change the href by taking the local url by developing a unique nomenclature.

I do not recommend doing this, because all your variables will have the same name to work.

    
18.06.2018 / 14:09