Lets say I have a "Student" model with functions getActive() and getInactive() like this:
public static function getActive()
{
return Student::with(['user', 'country', 'course', 'gender', 'language' => function($q){$q->orderBy('name');}])->where('active', 1)->get();
}
public static function getInactive()
{
return Student::with(['user', 'country', 'course', 'gender', 'language' => function($q){$q->orderBy('name');}])->where('active', 0)->get();
}
As you can see, the only difference is the >where('active', 0) at the end of each query.
I'm trying to declare a global "base query" to which I would append the where condition at the end. Something like this:
$baseQuery;
public function __construct()
{
$baseQuery = Student::with(['user', 'country', 'course', 'gender', 'language' => function($q){$q->orderBy('name');}]);
}
public static function getActive()
{
return $baseQuery->where('active', 1)->get();
}
public static function getInactive()
{
return $baseQuery->where('active', 0)->get();
}
This would not only save me redundant code, it would also make my code more clear and visible but as a newbie I'm struggling with the code.
Any ideas?
EDIT 1:
I'm calling it like this from my controller:
$students = Profile::getActive();