Skip to content
Advertisement

Laravel Eloquent Join Using Another table

Hi how to convert this SQL query to Laravel Eloquent?
I have three table: payments, devices and users. Payments table does not store user_id. but table has device_id. And device table also has user_id.
I want to get user name which made payment.

Advertisement

Answer

it’s very easy

Another way is:

We have three models:

  1. User (has many devices)
  2. Device(belongs to user && has many payments)
  3. Payment (belongs to device)

1) User.php

2) Device

3)Payment

and your query should be:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement