【问题标题】:Saving data into database using create function laravel使用创建函数 laravel 将数据保存到数据库中
【发布时间】: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)

标签: php laravel


【解决方案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

【讨论】:

  • Tks 但是我已经阅读过它的文档,我发现它有点难以理解。是否有其他资源可以提供更多示例?
  • 是的,可能有,您只需要搜索特定主题即可。如果发现有用,您可以接受作为答案:)。
  • $bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]);使用这个有什么限制吗?因为当我尝试添加另一个字段示例 $bear = Bear::create(['Name' => $Name, bearType=> $bearType, 'bearLocation' => $bearLocation]);它只显示名称和类型,而不是数据库中的位置
  • 哦 nvm 我忘了在我的模型中放入受保护的填充物哈哈哈 tks :)
  • 很高兴为您提供帮助:)
【解决方案2】:

你可以使用:

$bear = Bear::create(['Name' => $Name, 'bearType' => $bearType]);

欲了解更多信息,visit this link

【讨论】:

    【解决方案3】:

    你可以像这样插入数据:

    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

    【讨论】:

      【解决方案4】:

      假设后数组键与列名匹配,我会这样做。

      $bear = Bear::create($request->all()->except(['_token', 'type_of_fish']));    
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-25
        • 2020-01-15
        • 2020-06-09
        • 1970-01-01
        • 2019-05-27
        • 2021-07-18
        • 2016-12-13
        • 2019-07-12
        相关资源
        最近更新 更多