I am having so difficult time in developing an optimized query for the below code:
$categories = Category::with(['videos' => function($q) {
return $q->active();
} , 'videos.translations'])->latest('id')->limit(config('app.latest_limit'))->where('status_id', Status::Active)->get();
in my category and videos table i have below code:
public function scopeActive($q)
{
return $q->where('status_id', Status::Active);
}
public function getActive()
{
return $this->active()->get();
}
but if i do this,
$categories = Category::with(['videos' => function($q) {
return $q->active();
} , 'videos.translations'])->latest('id')->limit(config('app.latest_limit'))->getActive();
I get the below error,
Call to undefined method Illuminate\Database\Eloquent\Builder::getActive()
what worked for me as of now is:
$categories = Category::with(['videos' => function($q) {
return $q->active();
} , 'videos.translations' => function($q) {
return $q->active();
} ])->latest('id')->limit(config('app.latest_limit'))->active()->get();
also want to know how to optimize this more further if possible ...