Laravel 5 Pagination with Query Builder

前端 未结 5 1897
情歌与酒
情歌与酒 2021-01-05 10:18

I\'m making a Laravel Pagination based from my query result and be rendered in my view. I\'m following this guide http://laravel.com/docs/5.1/pagination but I get an error:<

5条回答
  •  北海茫月
    2021-01-05 10:31

    this is the way i did, it use query builder and get the same result with pagination

    $paginateNumber = 20;
    
        $key = $this->removeAccents(strip_tags(trim($request->input('search_key', ''))));
        $package_id = (int)$request->input('package_id', 0);
        $movieHasTrailer = MovieTrailer::select('movie_id')->where('status','!=','-1')->distinct('movie_id')->get();
    
        $movieIds = array();
    
        foreach ($movieHasTrailer as $index => $value) {
            $movieIds[] = $value->movie_id;
        }
    
    
        $keyparams = array();
        $packages = Package::select('package_name','id')->get();
    
        $whereClause = [
            ['movie.status', '!=', '-1'],
            ['movie_trailers.status', '!=', '-1']
        ];
    
        if(!empty($key)){
            $whereClause[] = ['movie.title', 'like', '%'.$key.'%'];
            $keyparams['search_key'] = $key;  
        }
    
        if($package_id !== 0){
            $whereClause[] = ['movie.package_id', '=', $package_id];
            $keyparams['package_id'] = $package_id;  
        }
    
    
    
        $movies = DB::table('movie')
                ->leftJoin('movie_package','movie.package_id','=','movie_package.id')
                ->leftJoin('movie_trailers','movie.id','=','movie_trailers.movie_id')
                ->where($whereClause)
                ->whereIn('movie.id',$movieIds)
                ->select('movie.*','movie_package.package_name','movie_trailers.movie_id as movie_id', 
                    DB::raw('count(*) as total_trailers, movie_id')
                )
                ->groupBy('movie.id')
                ->paginate($paginateNumber);
    

提交回复
热议问题