我需要在推送到Backpack的store方法中的请求中设置一个key => value;
在v3中,我有一个像这样的工作存储方法;
public function store(StoreRequest $request) {
$request->request->set('account_type', User::ACCOUNT_TYPE_BASIC);
$redirect_location = parent::storeCrud($request);
return $redirect_location;
}
但是为了保持开发项目的最新状态,我正在更新到v4,并且在尝试使用推荐的traitStore或traitUpdate方法时遇到了向$ request对象添加/删除任何问题的问题。文档。
这不起作用;
public function store(StoreRequest $request) {
$request->request->set('account_type', User::ACCOUNT_TYPE_BASIC);
$redirect_location = $this->traitStore();
return $redirect_location;
}
特别是,通过traitStore发送到数据库的请求中不包括'account_type'密钥,后者仅使用此Crud中(在这种情况下)setupCreateOperation()方法中定义的字段。
这里是否缺少我所需要的东西,或者我需要在需要操纵请求的任何地方而不是利用各种背包粗料方法来完全管理保存/更新?
问题可能是在v4中。 [getStrippedSaveRequest at the bttom of this class 有意删除该属性,因为它不是CRUD面板中的已注册字段
/**
* Returns the request without anything that might have been maliciously inserted.
* Only specific field names that have been introduced with addField() are kept in the request.
*/
public function getStrippedSaveRequest()
{
return $this->request->only($this->getAllFieldNames());
}
您可以通过如下方式解决此问题:在CRUD面板中将this属性添加为隐藏字段:
$this->crud->addField([
'name' => 'account_type',
'type' => 'hidden'
]);
现在该字段将不会显示在页面上,但是它将被注册,并且在创建过程之前将不再被删除。