/** * @param Tag $tag * @param Request $request * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|Paginator * @throws \Exception */ public function index(Tag $tag, Request $request) { $value = $request->get('value'); $locale = $request->get('locale'); $owner = $this->owner($request); $tags = $owner->tags->lists('id'); //why o why did we paginate this? $tags = $tag->with(['translations'])->where(function ($q) use($tags) { if (!empty($tags)) { $q->whereNotIn('id', $tags); } })->whereHas('translations', function ($q) use($value, $locale) { $q->where('locale', $locale); $q->where('name', 'like', '%' . $value . '%'); })->paginate(10); $sorted = new Collection($tags->items()); $sorted = $sorted->sortBy(function ($tag) use($locale) { return $tag->translate($locale)->name; }); $tags = new Paginator($sorted, $tags->perPage(), $tags->currentPage()); return $tags; }