我想在 laravel 應(yīng)用程序的 db 表中更新用戶的最后一行數(shù)據(jù)。我試圖使用 orderBy id desc limit 1 但它不起作用。DB::table('shortcontacts')->where('mobile',($data->mobile))->update(['otp_stts'=>'Verified'])->orderBy('id','desc')->first();
3 回答

PIPIONE
TA貢獻(xiàn)1829條經(jīng)驗 獲得超9個贊
您應(yīng)該將數(shù)據(jù)排序為 desc 然后像這樣更新:
DB::table('shortcontacts')->where('mobile',($data->mobile))->orderBy('id','desc')->first()->update(['otp_stts'=>'Verified']);

當(dāng)年話下
TA貢獻(xiàn)1890條經(jīng)驗 獲得超9個贊
最新和最舊的方法可讓您輕松按日期排序結(jié)果。默認(rèn)情況下,結(jié)果將按 created_at 列排序?;蛘?,您可以傳遞您希望排序的列名:
$last = DB::table('shortcontacts') ->latest() ->first();

月關(guān)寶盒
TA貢獻(xiàn)1772條經(jīng)驗 獲得超5個贊
您可以按降序排列模型的數(shù)據(jù),然后選擇第一個,如下所示:
$lastUser = App\User::orderBy('created_at', 'desc')->first();
在你可以像這樣更新你的模型之后:
$lastUser->name = "New Name";
$lastUser->save();
更新:您還可以使用 id 字段而不是“created_at”來訂購數(shù)據(jù)。
- 3 回答
- 0 關(guān)注
- 290 瀏覽
添加回答
舉報
0/150
提交
取消