I have a table Uuids
which has a morph relation with dealers, masterdealers, admins and customers
table. I need to get the email addresses from all the above mentioned user types tables along with their uuids from the uuids table. i'm using union to get all the emails from dealers, masterdealers, admins and customers table but I want to somehow eagerload their uuids too.
my desired output would be something like this :
{
email: 1@example.com
uuid: 1
}, {
email: 2@example.com
uuid: 5
}, {
email: 3@example.com
uuid: 11
}
I have tried:
DB::select("SELECT email FROM customers
UNION SELECT email FROM dealers
UNION SELECT email FROM masterdealers
UNION SELECT email FROM admins");
and it gives me all the emails from these 4 tables.
You need to join your morph table with in order to get uuids for your different models
SELECT u.uuid,c.email
FROM customers c
JOIN Uuids u ON c.id = u.some_mapped_col AND u.some_morph_col = 'Model Name'
UNION
SELECT u.uuid,d.email FROM dealers d
JOIN Uuids u ON d.id = u.some_mapped_col AND u.some_morph_col = 'Model Name'
UNION
SELECT u.uuid, m.email FROM masterdealers m
JOIN Uuids u ON m.id = u.some_mapped_col AND u.some_morph_col = 'Model Name'
UNION
SELECT u.uuid, a.email FROM admins a
JOIN Uuids u ON a.id = u.some_mapped_col AND u.some_morph_col = 'Model Name'
Also to run raw queries in laravel you might need DB::raw('.....')