Question: So I have a many to many relationship between Users and Photos via the pivot table `user_photo`. I use `belongsToMany('Photo')` in my User model. However the trouble here is that I have a dozen columns in my Photo table most I don't need (especially during a json response). So an example would be:

```
//Grab user #987's photos:
User::with('photos')->find(987);
//json output:
{
  id: 987,
  name: "John Appleseed",
  photos: {
    id: 5435433,
    date: ...,
    name: 'feelsgoodman.jpg',
    ....// other columns that I don't need
  }
}

```

Is it possible to modify this method such that `Photos` model will only return the accepted columns (say specified by an array `['name', 'date']`)?

**User.php**

```
public function photos()
{
  return $this->belongsToMany('Photo');
}

```

Note: I only want to select specific columns when doing a `User->belongsToMany->Photo` only. When doing something like `Photo::all()`, yes I would want all the columns as normal.

EDIT: I've tried [Get specific columns using "with()" function in Laravel Eloquent](https://stackoverflow.com/questions/19852927/get-specific-columns-using-with-function-in-laravel-eloquent?rq=1) but the columns are still being selected. Also <https://github.com/laravel/laravel/issues/2306>

Answer: 
You can use belongsToMany with select operation using laravel relationship.

```
public function photos()
{
  return $this->belongsToMany('Photo')->select(array('name', 'date'));
}

```
Specifying the exact columns you want for the `Photos` relationship will likely end up biting you in the butt in the future, should your application's needs ever change. A better solution would be to only specify the data you want to return in **that particular instance**, i.e. the specific JSON response you're delivering.

Option 1: extend/overwrite the `toArray()` Eloquent function (which is called by `toJson()`) and change the information returned by it. This will affect *every* call to these methods, though, so it may end up giving you the same problems as doing `select()` in the original query.

Option 2: Create a specific method for your JSON response and call it instead of the general `toJson()`. That method could then do any data building / array modifications necessary to achieve the specific output you need.

Option 3: If you're working with an API or ajax calls in general that need a specific format, consider using a library such as [League/Fractal](https://github.com/thephpleague/fractal), which is built for just such an occasion. (Phil is also working on a [book on building APIs](https://leanpub.com/build-apis-you-wont-hate), and it doesn't suck.)