我无法将其保存到数据库中。当我将数据提交到数据库时,它只会显示name_of_bear和所有关系内容(type_of_fish),但不会显示type_of_bear
有人可以向我解释为什么它不起作用,也许可以给我一个如何做的例子。谢谢
控制器:(这有效)
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $request->input('name_of_bear')]);
$bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);
return ('thank you');
}
但如果我这样做:
控制器:(不工作)
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $Name]);
$bear = Bear::create(['bearType' => $bearType]); --> doesn't work if add in this
$bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);
return ('thank you');
}
或者这个:
控制器:(不工作)
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $Name], ['bearType' => $bearType]); --> doesn't work
$bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);
return ('thank you');
}
答案 0 :(得分:1)
您可以像这样添加
$bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]);
完整代码:
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]);
return ('thank you');
} // removed extra }
有关详细信息,请阅读documentation
答案 1 :(得分:1)
答案 2 :(得分:1)
您可以插入如下数据:
public function submit(Request $request)
{
$data = array();
$data['type_of_fish']= $request->type_of_fish;
$data['Name'] = $request->Name;
$data['bearType'] = $request->bearType;
$bear = Bear::create($data);
return ('thank you');
}
有关详细信息,请参阅Documentation
答案 3 :(得分:0)
我会这样做,假设post数组键与列名匹配。
$bear = Bear::create($request->all()->except(['_token', 'type_of_fish']));