Problems With the hasPermissions method of Entrust in Laravel 5.6

-1

I'm using laravel and entrust and look at my driver's code >

public function edit($id)
{
    $role = $this->roleRepository->findWithoutFail($id);
    $permission=Permission::all();
    if (empty($role)) {
        Flash::error('Role not found');

        return redirect(route('roles.index'));
    }

    return   view('roles.edit')->with(['role'=>$role,'permission'=>$permission]);
}

And this is the view:

<select id="permission" type="text" class="form-control js-example-basic-multiple" name="permission"  multiple required autofocus>
    <option value=""></option>
    @foreach($permission as $per)

        <option value="{{$per->id}}"
           @if(isset($role))
               @if($role->hasPermission($per->name))
                   selected
               @endif
           @endif
        >{{$per->display_name}}</option>
    @endforeach
</select>

But when I look into it, it says: Method Illuminate \ Database \ Query \ Builder :: hasPermission does not exist. What am I doing wrong?

    
asked by Luis Dariel Valenciano 22.03.2018 в 15:45
source

2 answers

1

The function was good, but you should compare it to 1, I'll leave you the code tested in my local

<select id="permission" type="text" class="form-control js-example-basic-multiple" name="permission"  multiple="multiple" required autofocus>
<option value=""></option>
@foreach($permission as $per)
    <option value="{{$per->id}}" @if(isset($role)) @if($role->hasPermission($per->name) == 1) selected="selected" @endif @endif>
        {{$per->display_name}}
    </option>
@endforeach

Browser

    
answered by 22.03.2018 в 15:48
1

I already solved the problem is that since I'm using Infyom to generate the CRUDs it also creates a separate Model for the mapping and I was not using EntrustRoleTrait the solution is to put it in the new model use EntrustRoleTrait or just change the erencia instead of extend from Model extend of EntrustRole.

    
answered by 22.03.2018 в 16:09