mercredi 3 juillet 2019

Use withCount method with some condition

I have musics_rate table and musics table:

musics_rate:
    music_id : integer
    user_id : integer
    rate_type : boolean

Music model:

    public function rates()
    {
        return $this->belongsToMany(User::class, 'musics_rate');
    }

Now i want to sort musics by music_rates(rate_type==true) count (created in this weak):

    Music::where('created_at','>=', Carbon::parse('last saturday'))
           ->withCount('rates')
           ->orderby('rates_count','desc')
           ->get();

But it sort by all rates(positive rates and negative rates) count.

Is there any way to filter positive rates just?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire