yigitozmen
yigitozmen

Reputation: 957

Get count of votes with Eloquent in Laravel 4.2

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

Answers (1)

Emeka Mbah
Emeka Mbah

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

Related Questions