I have a small blogging app which allows users to upload photos and audio. Users can also search for other blogs. While searching, I'd like to add a ranking to my query with the following precedence:
1) Users with most photos
2) Users with most audio
User model is constructed as such:
public function blog() {
return $this->hasOne(Blog::class);
}
public function photos() {
return $this->hasMany(Photo::class);
}
public function audio() {
return $this->hasMany(Audio::class);
}
Blog model is constructed as such:
public function user()
{
return $this->belongsTo(User::class);
}
My current search query:
$blogs = Blog::where('description', 'ilike', '%'.$search.'%')
->orWhere('title', 'ilike', '%'.$search.'%')
->orWhereHas('user', function($query) use ($search) {
$query->where('name', 'ilike', '%'.$search.'%')
->orWhere('username', 'ilike', '%'.$search.'%');
})
->paginate(10);
Based off the given details, how do i adjust my query to return blogs ranked my their users photo and audio count?
** UPDATE **
I'm able to get the count of each nested relationship by using the withCount method with eager loading:
$blogs = Blog::where('description', 'ilike', '%'.$search.'%')
->orWhere('title', 'ilike', '%'.$search.'%')
->orWhereHas('user', function($query) use ($search) {
$query->where('name', 'ilike', '%'.$search.'%')
->orWhere('username', 'ilike', '%'.$search.'%');
})
->with(['user' => function($query){
$query->withCount(['blobs', 'audio']);
}])
->paginate(10);
However, how can i then order this current query by those nested count attributes?