Reputation: 957
i'm trying to get count of likes for each picture. Here is my code
$user = User::with(['pictures' => function($q){
$q->with('likes')->count();
}])
This code returns all likes as separately for each picture.
I think this count() is not working for me.
Thanks for help.
Upvotes: 0
Views: 646
Reputation: 17553
$user = User::with(['pictures' => function($q){
$q->where('likes',1)->count();
}]);
Remember to specify a relationship in User Model like so:
class Country extends \Eloquent {
public function pictures(){
return $this->hasMany('App\Models\Picture');
}
}
NB: I assumed App\Models\Picture is your pictures Model and also that 'likes' is Boolean datatype column in pictures table
Upvotes: 1