call method in laravel query

2

I have this query here:

SELECT p.descr, p.dt_hr, f.id_friend
FROM users u
JOIN friends f ON u.id = f.id_user
JOIN posts p ON p.id_user = f.id_friend
WHERE u.id = 1

It returns me this:

I'mwantingtospinintheforeachandmountanarraysomethinglikethis:

[0]=>['descr'=>$ln['descr'],<br>'dt_hr'=>$ln['dt_hr'],<br>'name'=>$this->getNameByIdFriend($ln['id_friend'])//pegaronomedesseusuário]

Inthismethod"getNameByIdFriend", I would make a select in another table by taking the id I passed to return the name to my position name of my array.

I do this using Zend Framework 2, I wanted to know how I can do it with laravel.

Thank you!

    
asked by anonymous 29.12.2016 / 14:34

1 answer

0

Using Laravel's Query Builder , your query should look like this:

DB::table('users')
    ->select('posts.descr, posts.dt_hr, friends.name')
    ->join('friends', 'users.id', '=', 'friends.id_user')
    ->join('posts', 'friends.id_friend', '=', 'posts.id_user')
    ->where('users.id', 1)
    ->get();

You should receive a Collection containing the results as objects in the StdClass class.

(It is assumed that the friends table has the name column.)

    
24.01.2017 / 03:17