Repository запрос с Left Join

есть запрос

$categories = DB::table('categories')
            ->LeftJoin('category_descriptions','category_descriptions.category_id','=','categories.id')
            ->select('categories.*','category_descriptions.name as category')
            ->where('category_descriptions.language_id','=', 1)
            ->orderBy('categories.id', 'ASC')->paginate(12);

как мне его перенести в репозиторий

модель Category

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [
        'image','parent','sort_order','published'
    ];

    public function description()
    {
        return $this->hasOne(CategoryDescription::class);
    }

    public function childrens()
    {
        return $this->hasMany(Category::class, 'parent')->with('description');
    }
}

модель CategoryDescription

class CategoryDescription extends Model
{
    protected $table = 'category_descriptions';

    protected $fillable = [
        'category_id','language_id','name','slug','description','meta_title','meta_description','meta_keyword'
    ];

    public function category(){
        return $this->belongsTo(CategoryDescription::class, 'category_id');
    }
}

есть интерфейс

interface CategoryRepositoryInterface
{
    public function getAllWithPaginateForAdmin(int $perPage, int $languageId);

    public function getById(int $id):Collection;
}

репозиторий

class CategoryRepository extends BaseRepository implements CategoryRepositoryInterface

{

protected $model;

public function __construct($model)
{
    $this->model = $model;
}

/**
 * @param int $perPage
 * @param int $languageId
 * @return mixed
 */
public function getAllWithPaginateForAdmin(int $perPage, int $languageId){

    return $this->model
                ->where('parent','=',0)->with(['childrens','description' => function($query) use ($languageId) {
                    $query->where('language_id', '=', $languageId);
                }])->paginate($perPage);
}

}

в итоге получается вот столько запросов, можно их уменшить? и верно ли я сделал репозиорий?


Ответы (0 шт):