lundi 30 mars 2020

How can i use scope in laravel query builder

I use laravel query builder instead Eloquent. Now, i have a lot of repeating joins. How can I use them without standard models scope in my query?

Now

public function getAllUserCourses(int $user_id)
{
    return $this->scopeQueryUserToCourse()
            ->where('user_course.user_id','=', $user_id)
            ->get();
}

public function getUserCourse($course_id,$user_id)
{
    return $this->scopeQueryUserToCourse()
            ->where('user_course.user_id','=', $user_id)
            ->first();
}

private function scopeQueryUserToCourse()
{
    return DB::table($this->table)
            ->select($this->table.'.id','title','description')
            ->join('user_course', $this->table.'.id','=','user_course.course_id')
            ->where('status','=', self::STATUS_ACTIVE_ID);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire