I have an application that manages shops, users and roles.
Each user could be an owner, supervisor, employee or admin.
The way to know if a user has admin, supervisor or employee permissions is through the role_user table that connects the users, the shops and the roles (the admin role sets NULL value in shop_id field on the role_shop table). The owner, instead, is obtained by the user_id field on the shops table.
I would like to perform a function on my user model that gets all the roles that a user has.
The following query works but I don't know how to perform it on Eloquent's Query Builder
SELECT priority, role, shop
FROM (
(
SELECT 1.5 AS priority, "owner" AS role, shops.id AS shop
FROM shops
WHERE shops.user_id = 1
)
UNION
(
SELECT roles.id AS priority, roles.name AS role, role_shop.shop_id AS shop
FROM role_shop
JOIN roles ON (roles.id = role_shop.role_id)
WHERE role_shop.user_id = 1
)
) AS result
ORDER BY priority
Thanks for your help folks!