【问题标题】:Laravel : How to update boolean value using foreign key in a tableLaravel:如何使用表中的外键更新布尔值
【发布时间】:2020-06-27 01:03:11
【问题描述】:

这是我要使用的数据库表 我想使用 user_id 将 form_submissions 表的参与列更新为布尔值 1

这是我的控制器

 public function update($id)
    {
        $submission = Form_Submission::find($id);
        $submission->participation=1;
        $submission->save();
        return back();
    {

我使用按钮从链接中获取了 user_id 并将其传递给函数 谁能告诉我如何使用 user_id 更新上述记录?

【问题讨论】:

    标签: database laravel foreign-keys boolean updates


    【解决方案1】:

    拥有user_id,您可以使用where 检索特定的Form_Submission,如下所示:

    Form_Submission::where('user_id', $user_id)->get();
    

    资源:https://laravel.com/docs/7.x/queries#where-clauses

    另外,要更新它,你可以使用 where - update,像这样:

    Form_Submission::where('user_id', $user_id)->update(['participation' => 1]);
    

    资源:https://laravel.com/docs/7.x/queries#updates

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2020-07-31
      • 2019-06-27
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 2017-04-30
      相关资源
      最近更新 更多