简体   繁体   中英

Converting simple nested select query to Laravel's Eloquent query

i am having this query with me which needs to be written in the eloquent form on internet not able to find exact solution for my problem.

SELECT 
    query_id, t1.time, result, platform_id
FROM
   query_logs t1
WHERE
    t1.time = (SELECT 
            MAX(time)
        FROM
            query_logs t2
        WHERE
            t1.query_id = t2.query_id);

i tried writing it as below i used query_Logs as model to my controller:

$bmdata = Query_Logs::select('query_id', 'time','result','platform_id')
              ->where('time', function($q){
              $q->from('query_logs')
               ->selectRaw('max(time)')
               ->where('query_id', '=', 'query_id')
           })
           ->get();

Can you guys help me with the same.

Use this:

$bmdata = Query_Logs::select('query_id', 'time', 'result', 'platform_id')
    ->where('time', function($q) {
        $q->from('query_logs as t2')
            ->selectRaw('max(time)')
            ->whereColumn('t2.query_id', '=', 'query_logs.query_id');
   })->get();

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