English 中文(简体)
登记在拉霍维尔的作用
原标题:Assigning role in Laravel with registration

早就对Laravel说了更新的话,而我正试图实现的是,在登记时将用户角色分配给用户。 我成功地开了种子,并创建了我的用户表,其中载有作用。 我建立了标识和标志,但我试图以某种理由建立登记册,以证明我从《URL para in my case:{>{> 东道Auth /register/1。 如果有的话,就应该发挥作用。

对我的登记册来说,这是目前如何看待:

public function rules(): array
{
    return [
         name  =>  required|string|max:150 ,
         email  =>  required|email|max:150|unique:users ,
         password  =>  required|confirmed ,
         role_id  =>  required 
    ];
}

public function getData(int $role_id): array
{

    $this->merge([
         role_id  => $role_id,
    ]);

    return $this->validated();
}

这里是我的主计长:

 public function __invoke(RegisterRequest $request)
{
    User::create($request->getData(1));
}

public function register(RegisterRequest $request, int $role_id)
{
    User::create($request->getData($role_id));
}

最后,网络路线(使用SPA):

Route::post( /register/{role_id} , [RegisterController::class,  register ]);

当我试图操作时,错误是:

  {
    "message": "The role id field is required.",
    "errors": {
        "role_id": [
            "The role id field is required."
        ]
    }
}

当我起作用时——我获得正确数目,但出于某种原因——我没有在我证实我的数据的那部分添加——只是我身体所听到的3个论点——是,我也是请求的我的机构:

{
"name": "test",
"email": "[email protected]",
"password": "password",
"password_confirmation": "password"
}

和我一样,我对拉拉夫说了不错的新话,这样,如果比我这样一耳不闻,或者如果有人能够帮助我了解我在这里做的错误。 预收:D

Update: I also tried this method:

Route::post( /register/registerCustomer , [RegisterController::class,  registerCustomer ]);
Route::post( /register/registerMechanic , [RegisterController::class,  registerMechanic ]);

原文:

public function registerCustomer(RegisterRequest $request)
{
    $role_id = $request->input( role_id , 1);

    User::create(array_merge($request->validated(), [ role_id  => $role_id]));
}

public function registerMechanic(RegisterRequest $request)
{
    $role_id = $request->input( role_id , 2);

    User::create(array_merge($request->validated(), [ role_id  => $role_id]));
}

萨米族问题:

"message": "SQLSTATE[HY000]: General error: 1364 Field  role_id  doesn t have a default value (Connection: mysql, SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (test, [email protected], $2y$12$bx3UZQqOG8cO91FTz5mOeeT/
问题回答

Try under >> 主要步骤是修改<代码>RegisterRequest,删除role_id的验证规则,因为它不属于申请机构。 然后,在控制器登记方法中,我建议从URL参数中提取role_id,并将其与经验证的要求数据合并。 这种办法确保,在创建有<条码>的新用户时,正确列入<条码>。 用户:create.

public function register(RegisterRequest $request, $role_id)
{
    $userData = $request->validated();
    $userData[ role_id ] = $role_id;

    User::create($userData);
}

您 路线

   Route::post( /register/{role_id} , [RegisterController::class,  register ]);

在使用制造方法之前,你将需要在模型类别上具体说明可填补或保住的财产。 这一错误可能是因为没有<代码>_id。 在那里。

摘自Laravel docs : 违约后,在开展大规模转让行动时,未计入可兑现阵列的属性无声被抛弃。 在生产过程中,这一行为是预期的;然而,在地方发展过程中,它可能导致混淆,说明模型变化为何没有生效。

If you wish, you may instruct Laravel to throw an exception when attempting to fill an unfillable attribute by invoking the preventSilentlyDiscardingAttributes method.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签