我有 2 个FormRequest
类(ReadersFormRequest
, SocialMediaFormRequest
),我想存储和更新一个Reader
. AReader
可能有 0 个或多个社交媒体帐户,因此有必要验证请求。
读者表格请求
class ReadersFormRequest extends FormRequest
{
public function rules()
{
return [
'first_name'=>'required',
'last_name'=>'required',
'birthday'=>'required',
'region'=>'required',
'photo_url'=>'required',
'support'=>'required',
'riwayas_id'=>'required',
'description'=>'required',
];
}
}
社交媒体表格请求
public function rules()
{
return [
'url'=>'required|url',
'title'=>'required'
];
}
所以我想在SocialMediaFormRequest
规则中合并ReadersFormRequest
规则
我找到了一个解决方案:
- 使 SocialMediaFormRequest 规则成为静态方法并将其合并到 SocialMediaFormRequest 规则中
社交媒体表格请求
public static function rules()
{
return [
'url'=>'required|url',
'title'=>'required'
];
}
读者表格请求
public function rules()
{
return array_merge(SocialMediaFormRequest::rules(),[
'first_name'=>'required',
'last_name'=>'required',
'birthday'=>'required',
'region'=>'required',
'photo_url'=>'required',
'support'=>'required',
'riwayas_id'=>'required',
'description'=>'required',
]);
}
我认为合并已正确完成,但是当我调用此更新控制器时ReadersFormRequest
,我不知道会发生什么。
public function update(ReadersFormRequest $request, Readers $reader)
{
// valid and update reader
Readers::whereId($reader->id)->update([
'first_name' => $request->validated()['first_name'],
'last_name' => $request->validated()['last_name'],
'photo_url' => $request->validated()['photo_url'],
'birthday' => $request->validated()['birthday'],
'region' => $request->validated()['region'],
'support' => $request->validated()['support'],
'riwayas_id' => $request->validated()['riwayas_id'],
'description' => $request->validated()['description']
]);
// For update their social media account links
foreach ($request->validated()['url'] as $key => $url)
{
}
return redirect(route('readers.show',$reader));
}
当我将阅读器放入表单时,不会调用此控制器。