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

birdietorerik's avatar

Rewrite my query to equalent

Hi!

I have not created this query as equalent query.

Is there som good equalent tutorials out there ?

Her is my query;

$query = DB::table('gpstrackers')
    ->select('gpstrackers.*', 'users.name')
    ->leftJoin('users', 'gpstrackers.player_id_ref', '=', 'users.id')
    ->whereIn('gpstrackers.id', function ($query) use ($from1, $to1, $golfID, $courceid, $status) {
        $query->selectRaw('MAX(gpstrackers.id) as id')
            ->from('gpstrackers')
            ->join('startlists_participants', 'gpstrackers.startliste_participant_ref_id', '=', 'startlists_participants.id')
            ->join('startlists', 'startlists_participants.startlist_id', '=', 'startlists.id')
            ->whereBetween('gpstrackers.regdate', [$from1, $to1])
            ->where('gpstrackers.golfclub_id', $golfID)
            ->where('startlists.status', $status)
            ->where('gpstrackers.courcegolfclub_id', $courceid)
            ->groupBy('gpstrackers.flight');
    })
    ->get();

0 likes
5 replies
tisuchi's avatar

@birdietorerik

It seems that the query in joining tables. If you have a chance, why not establish a relationship among tables so that you can easily access them?

⚠️ Fully acknowledge that I am not going deep into your codebase.

1 like
Randy_Johnson's avatar

If your code every starts to look like this, you need to stop and re-evaluate.

Learn eloquent, make your life easy.

P.S. eloquent isn't just one line of code, you have to do a few things to set it up, like writing your relationships inside your models.

1 like
jlrdw's avatar

That is equivalent to an eloquent query.

Eloquent has relation type queries as well as all query builder methods built in.

1 like

Please or to participate in this conversation.