I am trying to sort the table Posts
by the number of votes a received Post
. Votes are stored in another table.
(Votes: post_id, user_id, vote_type)
Post Model:
class Post extends Model
{
public function user()
{
return $this->hasOne(User::class);
}
public function votes()
{
return DB::table('votes')->where('post_id','=',$this->id)->sum('vote_type');
}
}
The vote functions return the number of votes received by mail (Voices a stored in a separate table)
Now I'm trying to order everything Posts
by the number of votes they received.
Post::get()->sortBy('votes');
This returns a follwing error:
The communication method should return an object of type Illuminate \ Database \ Eloquent \ Relations \ Relation
I would be grateful for any help to fix this!
source
share