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

bobmyles's avatar

Converting join sql statement to eloquent.

I have three models.

  1. ScheduledMessage points to table scheduled_messages.
  2. SubscribedService points to table subscribed_services.
  3. SubscriptionServicesMsisdn points to table subscription_services_msisdn.

I have raw sql query that I would want to convert into eloquent but can not think of a way.

SQL Query:

SELECT DISTINCT 
  b.message, c. phone_number, a.username, a.password from subscribed_services a 
inner join 
  subscription_services_msisdn c on a.id = c.subscribed_services_id 
inner join 
 scheduled_messages b on a.id = b.subscribed_services_id where DATE(b.scheduled_at) = CURDATE()

Would someone please help convert to eloquent. Thank you.

0 likes
8 replies
tykus's avatar

Here's my two cents for what it's worth... the result from this query is not Eloquent, i.e. it is not a Model instance nor a Collection of Model instances, it is something different. Use query builder instead.

bobmyles's avatar

Thank you tykus... Let try and convert it using query builder.

tykus's avatar
tykus
Best Answer
Level 104

Let's! This should be the equivalent of your raw query above:

DB::table('subscribed_services as a')
    ->join('scheduled_messages b', 'a.id', '=', 'b.subscribed_services_id')
    ->join('subscription_services_msisdnas 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();
jekinney's avatar

Eloquent doesn't do joins. It breaks your query into smaller queries. In theory smaller more efficient queries. What have you tried? Translating it for you doesn't really help you learn.

I am going to assume you have the relationships set on each model. And I abbreviated the model names: WARNING NOT TESTED and may need some tweaking.

 Service::with(['messages' => function($query) => {
    $query->whereDate('scheduled_at', CURDATE());
}], 'msisdn')->get();

https://laravel.com/docs/5.4/eloquent-relationships Relashionships (also get started and basic videos here)

https://laravel.com/docs/5.4/queries#where-clauses scroll to whereDate()

https://laravel.com/docs/5.4/eloquent-relationships#constraining-eager-loads Constraining eager loading

jekinney's avatar

@tykus Honestly, no joke intended, what's the benefit of that query over the raw sql? You pretty much just wrapped in in raw().

jlrdw's avatar

If your query works, use as is laravel has getPdo(). To run normal, regular queries.

$sql = "SELECT * from pets where petname like :petname";
        $sth = DB::connection()->getPdo()->prepare($sql);
        $sth->execute(array(':petname' => 'c%'));
        //$sth->execute();
        $results = $sth->fetchAll(\PDO::FETCH_ASSOC);
        print_r($results);
        foreach ($results as $row) {
            echo $row['petname'] . '<br>';
        }

Now of course this


        foreach ($results as $row) {
            echo $row['petname'] . '<br>';
        }

would not go in a controller, but view, this was just quick example of usage.

Please or to participate in this conversation.