'Using with method on Builder class
The code below works fine if we remove the with() method. But if we use with(), I get the following error. Why can't I use with() on the Builder class (::query)? Are there other alternatives? I have tried different solutions but couldn't seem to work.
Method Illuminate\Database\Eloquent\Collection::with does not exist.
public function getTaskDetails()
{
return ProductionTask::query()
->latest()
->whereUserCanSee()
->paginate()
->when($this->renderByDate, function ($query) {
return $query->sortBy('created_at');
})
->with([
'assignmentTests' => function ($q) {
if ($this->renderByRating) {
return $q->orderBy('rating', 'desc');
}
}
])->get();
}
Solution 1:[1]
As you are calling the pagination, it terminates the query. You can try the following code. Hope this will solve your problem.
public function getTaskDetails()
{
ProductionTask::whereUserCanSee()->latest()->when($this->renderByDate, function ($query) {
return $query->sortBy('created_at');
})->with(['assignmentTests' => function ($q) {
if ($this->renderByRating) {
return $q->orderBy('rating', 'desc');
}
}])->paginate(15);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dip Ghosh |
