我需要在推送到Backpack的存储方法中的请求中设置一个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()方法中定义的字段。
这里有没有我遗漏的东西,或者我需要完全管理保存/更新任何我需要操作请求的东西,而不是使用各种backpack crud方法?
发布于 2019-10-06 09:41:16
问题很可能是在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());
}
您可以通过将this属性作为隐藏字段添加到CRUD面板中来修复此问题,如下所示:
$this->crud->addField([
'name' => 'account_type',
'type' => 'hidden'
]);
现在,该字段不会显示在页面上,但它将被注册,并且在创建过程之前不会再被删除。
发布于 2019-11-15 13:23:34
store()不接受任何参数,因此您需要直接将属性添加到crud->请求中。
此外,您还可以动态添加字段,而不会在表单中创建隐藏字段。
public function store()
{
$this->crud->request->request->add('account_type', User::ACCOUNT_TYPE_BASIC);
$this->crud->addField(['type' => 'hidden', 'name' => 'account_type']);
$response = $this->traitStore();
return $response;
}
https://stackoverflow.com/questions/58226153
复制相似问题