I have a users
table like this:
I would like to display a query, where every user is listed twice with different text (created_at / updated_at) and the date. The merged query should be ordered by date (created_at and updated_at).
Expected result
Zoey created at 2022-10-09 ...
Peter created at 2022-10-20 ...
Zoey updated at 2022-10-24 ...
Peter updated at 2022-10-31 ...
How to do that in Laravel 8? How should the foreach
loop look like? Thanks!
2
Answers
In your controller(Using Eloquent ORM):
Then in your view file:
You can use Laravel’s
union
for this:This will return a Collection of User models like so:
Then you can loop this single Collection:
Output would be:
Also for reference, this would be the SQL Query executed: