保存前在模型中设置当前用户的user_id

问题描述 投票:0回答:1

我正在尝试使用Upgrade Guide从背包3.6更新到背包4.0。

更新之前,我在FolderCrudController中使用以下代码在保存模型之前根据当前登录的用户设置文件夹的user_id(外键):

public function store(StoreRequest $request)
{             
    // your additional operations before save here
    $request->request->set('user_id',backpack_user()->id); //set user id to currently logged user


    $redirect_location = $this->traitStore();
    // your additional operations after save here

    ....
}

这现在不再起作用,在保存模型的查询中未插入user_id:

SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败

在重大更新后如何保存之前如何设置模型的user_id

而且,我对文档中的此部分有点困惑:

步骤7。store()和update()方法先前存储了所有输入表格,特殊输入(如_token,_method,current_tab)除外等等。)。现在,此过程已更改:他们现在仅存储字段定义的输入。

这是什么意思?我现在是否必须以某种方式为我的user_id添加一个字段?

谢谢您的任何反馈。

php laravel backpack-for-laravel laravel-backpack
1个回答
0
投票

永远不要忘记包含:

use Illuminate\Http\Request;

然后使用请求方法找到ID:

public function store(StoreRequest $request)
{             
    // your additional operations before save here
    $request->request->set('user_id',$request->user()->id); //set user id to currently logged user


    $redirect_location = $this->traitStore();
   // your additional operations after save here
   ....
}
© www.soinside.com 2019 - 2024. All rights reserved.