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

bobmyles's avatar

Join Statement using Query builder

Hi there,

I have a Query builder statement below and I would want to introduce another condition to the query. However, I can not get this right.

This query works fine;

$content = DB::table('subscribed_services as a')
                ->join('scheduled_messages as b', 'a.id', '=', 'b.subscribed_services_id')
                ->join('subscription_services_msisdn as c', 'a.id', '=', 'c.subscribed_services_id')
                ->whereRaw('DATE(b.scheduled_at) = CURDATE()')
                ->selectRaw('b.message, c. phone_number, a.username, a.password')
                ->distinct()
                ->get();

I would want to introduce another where statement; this ->whereRaw('DATE(c.sent_time) != CURDATE()'). Meaning the query above should only pick values where subscription_services_msisdn.sent_time DATE(c.sent_time) IS NOT EQUAL to Current Date CURDATE().

Anyone lead me here. Regards.

0 likes
1 reply
bobmyles's avatar
bobmyles
OP
Best Answer
Level 1

I figured it out as below;

$content = DB::table('subscribed_services as a')
    ->join('scheduled_messages as b', 'a.id', '=', 'b.subscribed_services_id')
    ->join('subscription_services_msisdn as c', 'a.id', '=', 'c.subscribed_services_id')
    ->whereRaw('DATE(b.scheduled_at) = CURDATE()')
    ->whereRaw('DATE(c.sent_time) <> CURDATE()')
    ->selectRaw('b.message, c. phone_number, a.username, a.password')
    ->distinct()
    ->get();

Please or to participate in this conversation.