Laravel中fill()和update()之间的区别

时间:2020-03-19 19:02:30

标签: ajax laravel

我想在Laravel中使用Ajax更改表中的值。 但是,当我使用fill()时,返回值为“成功”,但是当我使用update()时,返回值为失败。 我的源代码如下。

    public function deleteCourse($id){
       $test = Course::find($id)->fill(['is_deleted' => 1])->save();
       $res = ['res' => 'success'];
       return json_encode($res);
    }
    public function deleteCourse($id){
       $test = Course::find($id)->update(['is_deleted'=>1])->save()'
       $res = ['res'=>'success'];
       return json_encode($res);
    }

2 个答案:

答案 0 :(得分:0)

在您对对象调用#txtLogin{ padding-top:10px; padding-bottom:10px; } .user_card { height: 460px; width: 420px; margin-top: auto; margin-bottom: auto; background: #DDE2E7; position: relative; display: flex; justify-content: center; flex-direction: column; padding: 10px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); -webkit-box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); -moz-box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); border-radius: 5px; } .brand_logo_container { position: absolute; height: 170px; width: 170px; top: -75px; border-radius: 50%; background: #fff; padding: 10px; text-align: center; } .brand_logo { height: 150px; width: 150px; border-radius: 50%; border: 2px solid white; } .form_container { margin-top: 100px; } .login_btn { width: 100%; background: #006298 !important; color: white !important; } #padding { padding-left: unset; padding-right: unset; } .login_container { padding: 0 2rem; } .input-group-text { background: #006298 !important; color: white !important; border: 0 !important; border-radius: 0.25rem 0 0 0.25rem !important; } 之前,填充不会保存到数据库中。

更新将立即更改数据库中的值。

在您的情况下,线路未正确终止。在末尾添加分号。

->save

答案 1 :(得分:0)

您不能在->save()之后调用->update(),因为它会返回boolean 1或0(成功或失败)。该记录直接在数据库中更新,因此不需要->save()

另一方面,

fill()在调用->save()之前不会将任何内容持久化到数据库,因此在该实例中是必需的。

public function deleteCourse($id){
  $test = Course::find($id)->fill(['is_deleted' => 1])->save();
  $res = ['res' => 'success'];
  return json_encode($res);
}

// OR

public function deleteCourse($id){
  $test = Course::find($id)->update(['is_deleted'=>1]);
  $res = ['res'=>'success'];
  return json_encode($res);
}
相关问题