How do I do this query with subselect in laravel?

0

I've tried it anyway, but it does not fit me SubSelect . I used the toSql() method to confirm and it really does not mount.

SQL

SELECT p.descr,p.dt_hr, (SELECT u1.name FROM users u1 WHERE u1.id = f.id_friend) AS name
FROM friends f JOIN users u ON u.id = f.id_user
               JOIN posts p ON p.id_user = f.id_friend
WHERE u.id = 1
ORDER BY p.dt_hr DESC
    
asked by anonymous 29.12.2016 / 21:41

1 answer

0

Solved!

$ posts = DB :: table ('friends') - >
select ('posts.dt_hr', 'posts.descr', 'name',
(DB :: raw ("(SELECT u1.name FROM users u1 WHERE u1.id = friends.id_friend) AS name")))
- > join ('users', 'users.id', '=', 'friends.id_user')
- > join ('posts', 'posts.id_user', '=', 'friends.id_friend')
-> where ('users.id', $ id_user)
- > orderBy ('posts.dt_hr', 'desc')
- > get ();

    
03.01.2017 / 13:01