I have this code in my model, but then using eloquent if I add a with('parent') the query stops working. Important side note this is not my code.
public function parent()
{
return $this->belongsTo(Category::class, 'category_id', 'id');
}
function scopeName($query, $request)
{
if ($request['query'] && $request["query"]["generalSearch"] != '') {
$query->orwhere('name', 'like', '%' . $request["query"]["generalSearch"] . '%');
}
}
function scopeOrder($query, $request)
{
if ($request['sort'] && $request["sort"]["field"] != '' && $request["sort"]["sort"] != '') {
$query->orderBy($request["sort"]["field"], $request["sort"]["sort"]);
}
}
This is the query that works so far.
function getCategories(Request $request)
{
$pagination = $request->query('pagination');
$categories = Category::Name($request)
->Order($request)
->paginate($request['pagination']['perpage'], ['*'], 'page', $pagination['page']);
}
Why can't I add a relationship to this query and how do I fix it?