Hello everyone,
It might be something trivial, but I could not find a way to make this elegant:
I have a Category model and a Picture model.
// in Category.php
public function pictures() {
return $this->hasMany('Picture')
}
// in Picture.php
public function Category() {
return $this->belongsTo('Category');
}
So far so good. What I want is on the controller resource index of category to have a column in the table to show the picture count. It's paginated, so I need the category count as well. I need to be able to order it by the picture count.
The use of the relationship as it is will involve ordering the collection after getting it from the database.
Using DB::query is easy, but I lack the category models afterwards.
What do you think will be the best Eloquent approach here?