Hi @winner1
It kind of does not make any sense to do it that way. You have a great explicit framework and yet decide to write pure SQL. I'm a beginner as well but it kind of hurts to read this code ;-)
You know that in Laravel you can chain queries before you finalise them with ->get(). You should look at local query scopes: https://laravel.com/docs/master/eloquent#query-scopes
So you would do something like that on your model:
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
public function scopeActive($query)
{
return $query->where('active', 1);
}
and then use it like so:
$users = App\User::popular()->active()->orderBy('created_at')->get();
You can also build dynamic scopes.
Hope it helps!