Create route template in Laravel with Resource

Asked

Viewed 220 times

0

I would like to know how to create a template in the Blade containing generic actions (view, edit, delete) for all routes of my application, for example in the column of actions 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 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 unique template for the buttons and avoid repeating codes and be able to control the directives in just one file.

For example, create a 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 don’t know how to implement the variables in the routes.

2 answers

1

You can create a php boot.blade.

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

I don’t recommend doing that, because all your variables will have the same name to work.

1


I imagine that’s about 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 slide imports the above file by passing the id (or any information you wish):

 @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'));
}
  • Perfect, saved a lot of code repetition so. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.