0

I would to convert mysql query to laravel query.

MySQL:

SELECT * FROM scadenze WHERE created_at IN(SELECT MAX(created_at) FROM scadenze GROUP BY processo_id)

Laravel ( i got an error, Object of class stdClass could not be converted to string):

DB::table('scadenze')->whereIn('created_at', DB::select("( SELECT MAX(created_at) FROM scadenze GROUP BY processo_id )"))->get();

2 Answers 2

2
DB::table('scadenze')->whereIn('created_at', function($query) {
   $query->from('scadenze')
         ->groupBy('processo_id')
         ->select(DB::raw('max(created_at)');
})->get();
Sign up to request clarification or add additional context in comments.

2 Comments

New error: Call to undefined method Illuminate\Database\Query\Expression::groupBy()
Try calling groupBy first then select, i've edited my answer
0

Try with this:

DB::table('scadenze')
    ->whereRaw(DB::raw("created_at IN (SELECT MAX(created_at) FROM scadenze GROUP BY processo_id)"))
    ->get();

I hope this can help!

2 Comments

I insert variable into dd(); but i got another error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT MAX(created_at) FROM scadenze GROUP BY processo_id' at line 1 (SQL: select * from scadenze where created_at IN SELECT MAX(created_at) FROM scadenze GROUP BY processo_id)
I've updated my answer, missing parentheses in the IN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.