cafonso
cafonso

Reputation: 909

Laravel - Selecting specific columns on many-to-many relationships

I have two models in my Laravel 4.2 web application, User and Group. A user can be a member of many groups, and a group can have many members. Both models are thus joined with a many-to-many relationship:

<?php
    class User extends Eloquent {
        public function groups()
        {
            return $this->belongsToMany('Group');
        }
    }

    class Group extends Eloquent {
        public function users()
        {
            return $this->belongsToMany('User');
        }
    }
?>

One of my API resources is /groups, which lists all groups available within the app:

<?php
    $groups = Group::with('users')->all();
?>

This works, however in the JSON response each user contains all fields from the users table (excluding of course those in the $hidden attribute). I would like this relationship to return only a specific set of fields instead of the whole table.

In other relationship types I can easily achieve this with the following statement (assume now that users may belong to only one group):

<?php
    public function users()
    {
        return $this->hasMany('User')->select(['id', 'first_name', 'last_name']);
    }
?>

However the above does not seem to work with many-to-many relationships. I came across this question which apparently refers to the same issue and it looks like this was not possible in Laravel 4.1. The author of the chosen answer, tptcat, provides a link to an issue on Laravel's Github issue tracker, but the link is no longer working and I couldn't figure whether this issue is still open in 4.2.

Has anybody come across this and successfully managed to solve it?

Upvotes: 12

Views: 7847

Answers (4)

Tharindu Mahesh
Tharindu Mahesh

Reputation: 41

{
   return $this->belongsToMany('User')->select(array('id', 'name'));
}

use this

Upvotes: 3

Masood Khan
Masood Khan

Reputation: 635

Instead of selecting column in relationship, you can select column as below:

$groups = Group::with('users:id,first_name,last_name')->all();

And when you are selecting column in relationship, make sure that you are selected foreign key of relation table

Upvotes: 0

Vivek Pandey
Vivek Pandey

Reputation: 406

Use like this.

<?php
    class User extends Eloquent {
        public function groups()
        {
            return $this->belongsToMany('Group')->select(array('id', 'name'));
        }
    }

    class Group extends Eloquent {
        public function users()
        {
            return $this->belongsToMany('User')->select(array('id', 'name'));
        }
    }
?>

Upvotes: 0

user3774430
user3774430

Reputation:

The all method takes in an array of column names as a parameter.

If you look at the source, it takes * (which means everything) by default.

https://github.com/laravel/framework/blob/4.2/src/Illuminate/Database/Eloquent/Model.php#L624-L629

You can pass in the columns that you needed and it should return the results only with the specified columns.

<?php

$groups = Group::with('users')->all(array('first_column', 'third_column'));

Upvotes: 0

Related Questions