How do I achieve something like this using Laravel Eloquent? I didn't find how to name the same table twice using Eloquent.
Thanks.
Table users
id | first_name | last_name |
---+------------+-----------+
1 | John | Doe |
2 | Jane | Doe |
3 | Some | Name |
Table stamp
id | date | applicant_id | app_by_id |
---+------------+---------------+-----------+
1 | 2013-03-15 | 1 | 2 |
2 | 2013-03-10 | 2 | 3 |
3 | 2013-03-13 | 2 | 1 |
What I want to show:
date | applicant | app_by |
------------+-----------+-----------+
2013-03-15 | John Doe | Jane Doe |
2013-03-10 | Jane Doe | Some Name |
2013-03-13 | Jane Doe | John Doe |
Desired equivalent SQL query:
SELECT s.date,
CONCAT_WS(' ', NULLIF(u1.first_name, ' '), NULLIF(u1.last_name, ' ')) AS applicant,
CONCAT_WS(' ', NULLIF(u2.first_name, ' '), NULLIF(u2.last_name, ' ')) AS app_by
FROM stamp s
LEFT JOIN users u1 ON s.applicant_id = u1.id
LEFT JOIN users u2 ON s.app_by_id = u2.id
belongsToManyrelationship and deal with joining first and last names in the program layer? Why don't you usejoin(inner` but wantleft joins? Why do you want to use Eloquent for something that it isn't supposed to work with?