简体   繁体   中英

laravel DB::raw SELECT WITH WHERE CLAUSE

i have order table that a user may have many rows in it so i want to get the status of the latest order by the current user

I want to get the status of the row that has the max id where user id is equal to the current auth user id,

table structure:

order(id,user_id,item_id,....,status)

I think you should try this:

$currentUserId = Auth::user()->id;

DB::table('order')->select('status')->where('user_id',$currentUserId)->orderBy('created_at','desc')->get();

Hope this work for you

Try this.

$userId = Auth::user()->id;
DB::table('order')->orderBy('created_at','desc')->select('status')->where('user_id',$userId)->get();

SQL Query

SELECT o.status FROM order AS o WHERE o.user_id=? ORDER BY o.id DESC LIMIT 0,1;

Laravel

$user_id = 123;
DB::table('order')
    ->select(DB::raw('status'))
    ->where('user_id', '=', $user_id)
    ->orderby('id', 'DESC')
    ->first();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM