如何使用 laravel eloquent 通过 where 子句将数据插入数据库?

How to insert data into db with where clause using laravel eloquent?

我正在使用这个语句来插入数据。

if(\App\tempLogin::insert(['otp'=>$otp])->where('mobile','=',$mobile))
  return 1;

但是我得到一个错误"Field 'username' doesn't have a default value (SQL: insert into tempLogin (otp) values (8673))"

您需要使用update()方法来更新现有数据库行中的信息:

\App\tempLogin::where('mobile', $mobile)->update(['otp' => $otp]);

insert() 创建新行。