search

Home  >  Q&A  >  body text

I use a third-party search package in Laravel, but I don’t know how to paginate the search results?

My problem is that I want to paginate the search results, but laravel's paging is based on Elouqent and the database query builder. I don't know how to implement it in the controller and front-end view.

This is the code to do a database search:

$article = Searchy::companys('title','body')->query($request->seek)->get();

Please give me some advice.

伊谢尔伦伊谢尔伦2751 days ago517

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-05-16 16:53:28

        public function all(Request $request)
        {
            $user = User::latest()->get();
            foreach ( $user as $key=>$value){
                $company = Company::find($value->company_id);
                $title = CompanyTitle::find($value->title_id);
                $value['titleName'] = $title->title;
                $value['companyName'] = $company->name;
            }
            $size = 8;
            if (empty($request->page)){
                $page = 1;
            }
            else{
                $page = $request->page;
            }
            $users = $user->toArray();
            $item = array_slice($users, ($page - 1) * $size,$size);
            $arr =new LengthAwarePaginator($item, count($user), $size, $page, [
                'path' => Paginator::resolveCurrentPath(),  //注释2
                'pageName' => 'page',
            ]);
            return $arr;
        }

    This is a blog I read myself, which solved the problem of no instantiation and no link. When writing code, the page number was not processed, which is not good code. . .
    Blog address Laravel manual paging implementation

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 16:53:28

    Use Illuminatepagniate or lengthawaredpagniate.

    Then for new Paginate(), you need to check the incoming parameters in the manual

    reply
    0
  • 为情所困

    为情所困2017-05-16 16:53:28

    You don’t need to get, just use pagination directly after the query

    reply
    0
  • Cancelreply