I have two related models:
class Attribute extends Eloquent
{
public function categories()
{
return $this->hasMany('Category');
}
}
class Category extends Eloquent
{
public function attributes()
{
return $this->belongsTo('Attribute');
}
}
I want to return all attributes and their categories as a JSON object, but I only want to select certain fields in both models (i.e. not return fields like 'created_at' in the JSON).
I've tried this:
$attributes = Attribute::select('id', 'name')
->with(['categories' => function ($query) {
$query->select('categories.id', 'categories.name', 'categories.attribute_id');
}]);
Response::JSON($attributes->get());
but despite the select query for the related model, unrequested fields are returned:
attributes: [{
id: 3,
name: "Organisation",
categories: [{
id: 3,
name: "Asia HQ",
attribute_id: 3,
created_at: "2013-11-30 00:00:00",
updated_at: "2013-11-30 00:00:00"
}]
}]
How do I select only certain columns in related model when eager loading?