12

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?

4 Answers 4

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

use this

Sign up to request clarification or add additional context in comments.

Comments

0

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'));

2 Comments

Yes, this does work. However, if I make similar queries in different actions throughout my controller I have to pass the same array to all of them. I was aiming for a solution similar to what can be done with other kinds of relationships.
You can always create a function that does these common queries in your model and call it from the different actions in your controllers.
0

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'));
        }
    }
?>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.