1

I followed a tutorial on how to use roles, permissions... with a laravel app. I am using entrust package this the interface :

Interface

But, I am facing 2 errors, (i ll talk about both of them in the same topic, cause I thought maybe they caused by same thing) :

1) ErrorException in Gate.php line 317: Illegal offset type in isset or empty

--> this happens when I click on "items" or "roles"

2) FatalErrorException in UserController.php line 88: Call to a member function pluck() on null

--> It happens when I try to edit a user

This it Usercontroller :

 //....
public function edit($id)
{
    $user = User::find($id);
    $roles = Role::pluck('display_name','id');
    $userRole = $user->roles->pluck('id','id')->toArray();
    return view('users.edit',compact('user','roles','userRole'));
}

public function update(Request $request, $id)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email|unique:users,email,'.$id,
        'password' => 'same:confirm-password',
        'roles' => 'required'
    ]);

    $input = $request->all();
    if(!empty($input['password'])){ 
        $input['password'] = Hash::make($input['password']);
    }else{
        $input = array_except($input,array('password'));    
    }

    $user = User::find($id);
    $user->update($input);
    DB::table('role_user')->where('user_id',$id)->delete();


    foreach ($request->input('roles') as $key => $value) {
        $user->attachRole($value);
    }

    return redirect()->route('users.index')
                    ->with('success','User updated successfully');
}

and this is ItemController :

public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'required',
        'description' => 'required',
    ]);

    Item::create($request->all());

    return redirect()->route('itemCRUD2.index')
                    ->with('success','Item created successfully');
}

public function show($id)
{
    $item = Item::find($id);
    return view('ItemCRUD2.show',compact('item'));
}

public function edit($id)
{
    $item = Item::find($id);
    return view('ItemCRUD2.edit',compact('item'));
}

and this is RoleController :

  public function index(Request $request)
{
    $roles = Role::orderBy('id','DESC')->paginate(5);
    return view('roles.index',compact('roles'))
        ->with('i', ($request->input('page', 1) - 1) * 5);
}

public function create()
{
    $permission = Permission::get();
    return view('roles.create',compact('permission'));
}

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required|unique:roles,name',
        'display_name' => 'required',
        'description' => 'required',
        'permission' => 'required',
    ]);

    $role = new Role();
    $role->name = $request->input('name');
    $role->display_name = $request->input('display_name');
    $role->description = $request->input('description');
    $role->save();

    foreach ($request->input('permission') as $key => $value) {
        $role->attachPermission($value);
    }

    return redirect()->route('roles.index')
                    ->with('success','Role created successfully');
 }

 public function show($id)
 {
    $role = Role::find($id);
    $rolePermissions = Permission::join("permission_role","permission_role.permission_id","=","permissions.id")
        ->where("permission_role.role_id",$id)
        ->get();

    return view('roles.show',compact('role','rolePermissions'));
 }
 public function edit($id)
 {
    $role = Role::find($id);
    $permission = Permission::get();
    $rolePermissions = DB::table("permission_role")->where("permission_role.role_id",$id)
        ->pluck('permission_role.permission_id','permission_role.permission_id');

  return view('roles.edit',compact('role','permission','rolePermissions'));
 }

the model : User.php

class User extends Authenticatable
{
use Notifiable;

protected $table = 'users';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

Could anyone help me to solve this ? thanks in advance.

Updates :

After changing the driver to array in my .env file. CACHE_DRIVER=array I am getting this when clicking on items or roles. Trying to find out how to attribute roles to users now !!

enter image description here

Naj
  • 119
  • 1
  • 2
  • 13

1 Answers1

2

You need to add the correct relations to your user model before you can load them. You are trying to load the property roles from you User model, but this doesn't exist so it returns null.

The EntrustUserTrait has to be added to your User model, as defined here.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • This is working ! Thank you. One error has been solved. To fixe the other one, I changed the driver to array in my .env file. CACHE_DRIVER=array. Now when I click on roles or items I got the following message : You don't have permission as you can see in the image above. Now trying to find out how to attribute roles to users – Naj Jan 24 '17 at 16:03
  • You should change this in the database. Also, this is a second question, you should close this question and create a new one for the other question. – Jerodev Jan 24 '17 at 16:10