简体   繁体   English

Spatie laravel许可导致参数1错误通过

[英]Spatie laravel-permissions Giving Error of Argument 1 Passed

I'm working with the Spatie laravel-permissions package. 我正在使用Spatie laravel-permissions软件包。 When I create my role I want to assign permissions to roles but it's returning the 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 传递给Spatie \\ Permission \\ Exceptions \\ GuardDoesNotMatch :: create()的参数1必须为字符串类型,给定为null,在C:\\ xampp \\ htdocs \\ blog \\ vendor \\ spatie \\ laravel-permission \\ src \\ Traits \\ HasPermissions中调用.php行505

Controller 调节器

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!');
}

Blade

<div class='col-lg-10 col-lg-offset-4'>
    <h1><i class='fa fa-key'></i> Add Role</h1>
    <hr>
    <form action="{{action('RoleController@store')}}" method="post" enctype="multipart/form-data">
        {{ csrf_field() }}
        <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="{{ $permission->id }}">
                <label class="form-check-label" for="defaultCheck1">
                    {{$permission->name}}
                </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>

Can someone help me solve this problem? 有人可以帮我解决这个问题吗?

This is the screenshot 这是截图 DD($请求 - >权限);

Solution: 解:

$guard_name of the Model is different or NULL from the $Request make sure both are in the same Guard 'web' / 'api' 模型的$ guard_name与$ Request不同或为NULL,请确保两者都在同一个Guard'web'/'api'中

// Solution by: @VardanaBhanot in comments //解决方案:@VardanaBhanot在评论中

RoleController.php RoleController.php

public function store(Request $request)
{
    // validate role 'name' and make sure that's unique within 'roles' table
    // upon creating a role we want to make sure that it's not an empty role
    // so permissions are required
    $this->validate($request, [
        'name' => 'required|unique:roles|max:10',
        'permissions' => 'required'
      ]);

    // create the role by given name and assign it to @var $role
    $role = Role::create(['name' => $request['name']]);

  // since we are using Validator to make sure "permissions" exists 
  // we can skip the "isset($request['permission'])" call.
  foreach($request['permissions'] as $permission)
  {
    if($p = Permission::where('id', '=', $permission)->first())
    {
      $role->givePermissionTo($p);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spatie/Laravel-权限:虚拟数据错误 - Spatie/Laravel-Permissions: Dummy Data Error Spatie/Laravel-Permissions:未定义的方法 translationEnabled() 错误 - Spatie/Laravel-Permissions: Undefined method translationEnabled() error Laravel spatie/laravel-permissions 的问题(HasRole 在 Model 用户中) - Problem with Laravel spatie/laravel-permissions ( HasRole in Model User ) 尝试使用Spatial Laravel权限hasRoles进​​行多重身份验证 - Trying to do multi auth using spatie laravel-permissions hasRoles spatie / laravel-permissions AssignRole方法导致错误[违反完整性约束:1452无法添加或更新子行] - spatie/laravel-permissions assignRole method causes error [Integrity constraint violation: 1452 Cannot add or update a child row] spatie 错误:PermissionServiceProvider 未加载(Laravel 权限) - spatie error : PermissionServiceProvider Not Loaded (Laravel Permissions) Laravel在spatie权限中的foreach循环中给出错误 - Laravel giving error in foreach loop in spatie-permission “用户未登录”-Spatie权限错误 - “User is not logged in”- Spatie Permissions Error Laravel 5.7 + Spatie 权限 + JWT 身份验证 - Laravel 5.7 + Spatie Permissions + JWT auth Laravel 发送电子邮件,错误:参数 2 传递给 - Laravel send email, error: Argument 2 passed to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM