【发布时间】:2018-03-22 18:23:21
【问题描述】:
我无法将其保存到数据库中。当我将数据提交到数据库时,它只会显示 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');
}
【问题讨论】:
-
试试
$bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]); -
如果您需要,还有另一种方法可以将数据插入数据库,我可以与您分享
-
@Jigar 是的,非常感谢。当然,我很想知道更多(对 Gaurav)