I have two tables: User and Post . One User can have many posts and one post belongs to only one user .
In my User model, I have a hasMany relationship ...
public function post(){ return $this->hasmany('post'); }
And in my post model, I have belongsTo relation ...
public function user(){ return $this->belongsTo('user'); }
Now I want to combine these two tables using Eloquent with() but I want the specific columns from the second table. I know that I can use Query Builder, but I do not want to.
When in the Post model I write ...
public function getAllPosts() { return Post::with('user')->get(); }
It runs the following queries ...
select * from 'posts' select * from 'users' where 'users'.'id' in (<1>, <2>)
But what I want is ...
select * from 'posts' select id,username from 'users' where 'users'.'id' in (<1>, <2>)
When i use ...
Post::with('user')->get(array('columns'....));
Returns only the column from the first table. I want specific columns using with() from the second table. How can i do this?
php orm eloquent laravel laravel-query-builder
Awais Qarni Nov 08 '13 at 6:30 2013-11-08 06:30
source share