Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bohowe's avatar

Raw query

Hi,

How can I convert this query to Eloquent?

SELECT *, (select ga.score FROM game_activities as ga WHERE ga.creator_id = game_activities.creator_id AND ga.type=0 AND ga.game_id = game_activities.game_id) as start_score FROM `game_activities` WHERE game_id=37 and type=2 GROUP BY creator_id

Thanks in advanced!

0 likes
1 reply
bobbybouwmann's avatar
Level 88

Something like this should put you in the right direction

$gameActivities = GameActivity::select([
    '*', 
    \DB::raw('(select ga.score FROM game_activities AS ga WHERE ga.creator_id = game_activities.creator_id AND ga.type = 0 AND ga.game_id = game_activities.game_id) as start_score')
])
    ->where('type', 2)
    ->where('game_id', 37)
    ->groupBy('creator_id')
    ->get();

Please or to participate in this conversation.