Limit the elements that users can see in the views in Laravel 5.5

0

This is the view of posts on a blog to create, edit, view and delete. I want to restrict the create and delete button so that a user with a certain id can be the only one to see and use these buttons.

Here I put the code of the view:

<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">

                <div class="panel-heading">
                    Lista de Entradas
                    <a href="{{ route('posts.create') }}" 
                    class="btn btn-sm btn-primary pull-right">Crear</a>
                </div>

                <div class="panel-body">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th width="10px">ID</th>
                                <th>NOMBRE</th>
                                <th colspan="3">&nbsp;</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($posts as $post)                               
                            <tr>
                                <td>{{ $post->id }}</td>
                                <td>{{ $post->name }}</td>
                                <td width="10px">
                                <a href="{{ route('posts.show', $post->id) }}" class="btn btn-sm btn-default">ver
                                </a>
                            </td>
                            <td width="10px">
                                <a href="{{ route('posts.edit', $post->id) }}" class="btn btn-sm btn-default">editar
                                </a>
                            </td>
                            <td width="10px">
                                {!! Form::open(['route' => ['posts.destroy', 
                                $post->id], 'method' => 'DELETE']) !!}
                                    <button class="btn btn-sm btn-danger">
                                        Eliminar                                    
                                    </button>
                                {!! Form::close() !!}
                            </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                    {{ $posts->render() }}
                </div>
                </div>  
            </div>
        </div>
    </div> 

This is what the user should see with a certain id that allows him to see the buttons:

And this is what the rest of the users should see:

That is, basically it is to prevent users from creating or deleting except for a specific user with a certain id.

Edit1: I was able to fix it by adding this conditional in the view:

@if(Auth::user()->id == 29)

                    <a href="{{ route('posts.create') }}" 
                    class="glyphicon glyphicon-plus btn 
btn-sm btn-primary pull-right"> Crear</a>

                    @endif

In this way only the user with that specific id can create. The same case would be to eliminate.

    
asked by Kinafune 25.02.2018 в 18:32
source

2 answers

1

The simple implementation that you want to achieve can be done like this:

@if(Auth::user()->id == 1 )
    //Mostrar botón
@endif

Personally, I would implement a "Role" N to N class with User. I would assign roles to users and then show or not the button based on that role, for example something like this:

@if(Auth::user()->tieneRole("Admin"))
    //Mostrar botón
@endif

In which Role has a function that you have to create in the User model that checks that user has that role.

    
answered by 01.03.2018 в 16:27
0

I recommend you use Entrust for Laravel.

It is a very easy and very complete permissions and roles system.

Blade role management

 @role('admin')
   //Cualquier contenido blade
 @endrole

Blade permission management

@permission('customer-list')
    //cualquier contenido blade
@endpermission

In the documentation you have everything well explained.

    
answered by 26.02.2018 в 09:25