VETO87's avatar

pagination

I have two issues first, How can I use pagination In ($Instructors = $university->insturctors;) ? Should I make it like this ($Instructors = Instructor::where('university_id', $university->id)->paginate(5);)

second, after making pagination work when user search and go to the second or third page my search function fall and all Instructors appear

    public function create (Universities $university) {
        $Instructors = $university->insturctors;

        if (request('search')){

            $Instructors = $university->insturctors()->when(request('search'), function ($builder, $search) {
                $builder->where('name', 'like', '%'. $search . '%');
            })->paginate(3);        
       }

        return view('university', [
            'University' => $university,
            'insturctors' => $Instructors,
        ]);
    }
0 likes
4 replies
VETO87's avatar

@MichalOravec Thank You it work, Can you help me with my last Question. How can I paginate array ? $Instructors = $arranged; $arranged is array that contains my Instructors with some custom edits. I can show you the code why did i use array but it's like 50 lines.

VETO87's avatar

@jlrdw I found this method to paginate my array

    public function create()
    {
            $Instructors = $this->paginate($arranged);
              return view('college', [
            'Instructors' => $Instructors,
        ]);
    }
    public function paginate($items, $perPage = 2, $page = null, $options = [])
    {
        $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
        $items = $items instanceof Collection ? $items : Collection::make($items);
        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, 
         $page, $options);
    }

Which seems the best solution but there's tiny issue when I try to go to the second page or third ... it redirect me to http://localhost/?search=john&page=2 it should be like this http://localhost/KAU/FCIT?search=john&page=2

Please or to participate in this conversation.