mercredi 23 janvier 2019

Spatie laravel-permissions giving error of Argument 1 passed

I am working on Spatie laravel-permissions. when I created my role crude so I want to assign permissions to roles but its giving a error Argument 1 passed to Spatie\Permission\Exceptions\GuardDoesNotMatch::create() must be of the type string, null given, called in C:\xampp\htdocs\blog\vendor\spatie\laravel-permission\src\Traits\HasPermissions.php on line 505

public function store(Request $request) {
    //Validate name and permissions field
        $this->validate($request, [
            'name'=>'required|unique:'.config('permission.table_names.roles').'|max:10',
            ]
        );

        $name = $request['name'];
        $role = new Role();
        $role->name = $name;

        $permissions = $request->permissions;
        $role->save();
    //Looping thru selected permissions
        foreach ($permissions as $permission) {

            $p = Permission::where('id', '=', $permission)->firstOrFail(); 
         //Fetch the newly created role and assign permission
            $role = Role::where('name', '=', $name)->first(); 
            $role->givePermissionTo($p);
    }

        return redirect()->route('roles.index')
            ->with('flash_message',
             'Role'. $role->name.' added!'); 
    }

this is the blade code

    <div class='col-lg-10 col-lg-offset-4'>

    <h1><i class='fa fa-key'></i> Add Role</h1>
    <hr>

    <form action="" method="post" enctype="multipart/form-data">
      
      <div class="form-group row"  style="margin:5%;">

            <label for="name" class="col-sm-2 col-form-label">Name *</label>

            <input type="text" class="form-control col-sm-10" id="name" name="name"
             placeholder="Enter your Role Name"/>

      </div>

    <h5><b>Assign Permissions</b></h5>

    <div class="form-check">
      @foreach ($permissions as $permission)
  <input class="form-check-input" type="checkbox" name="permissions[]" value="">  
  <label class="form-check-label" for="defaultCheck1">
    
  </label>
  @endforeach
</div>
    <div class="form-group row"  style="margin:5%;">

            <button type="submit" class="btn btn-primary col-sm-3 col-sm-offset-3">Add Role</button>
      </div>
</form>
</div>

So if someone could help me solve this problem



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire