Hi this will be made easily using Eloquent relations:
create upvote, downvote models using Artisan:
php artisan make:model upvote
php artisan make:model downvote
//Add this to app/User.php
//User Upvotes relation
public function upvotes()
{
return $this->hasMany(Upvote::class);
}
//User Downvotes relation
public function downvotes()
{
return $this->hasMany(Upvote::class);
}
//Add this to your script
$user = User::with(['upvotes' => function($query){
$query->where('upvotes.voteOn', 2);
$query->orderBy('upvotes.created_at');
}, 'downvotes' => function($query){
$query->where('downvotes.voteOn', 2);
$query->orderBy('downvotes.created_at');
}])->orderBy('created_at')->get();