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.
Jul 4, 2017
8
Level 1
Converting join sql statement to eloquent.
I have three models.
- ScheduledMessage points to table scheduled_messages.
- SubscribedService points to table subscribed_services.
- 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.
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();
Please or to participate in this conversation.