0
votes

how can I choose which fields I want to get from the with ORM eloquent. For example

$tourTeams = Tournament::with('teams')->where('id', $tourId)->first();

From the teams relation I want only to get the name (without the id and timestamps).

I didn't it in the documentation. For the Tournament eloquent I can do it via the get function while passing it an array of fields names, like this: get(array('name', 'id')). But how do I do this on the Team eloquent?

Note: here is how Team related to Tournament, this code taken from the Tournament eloquent file:

public function teams() { return $this->belongsToMany('Team', 'Tournament_Team'); }

1
This is a duplicate of stackoverflow.com/questions/23709936/… I just tested this on 4.2 and it still doesn't work.user1669496
There's a pull request addressing this issue since 4.1, but Taylor didn't merge that yet.Jarek Tkaczyk

1 Answers

1
votes

You can get specific columns from the relation like this:

 $tourTeams = Tournament::with(['teams'=>function($q){
         $q->select('id','name');
 }])->where('id', $tourId)->first();