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

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

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

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