Laravel Eloquent Pagination


Laravel Eloquent Pagination

 /**
 * Route : scripts.list
 *
 * @param  Request  $request
 * @return view
 */
public function index(Request $request)
{   
    $scripts = ScriptModel::select('*');
    if($request->get('search'))
    {
        $search = $request->get('search');
        $scripts = $scripts->where(function ($query) use ($search) 
        {
            return $query->where('title', 'like', '%'.$search.'%')->orWhere('description', 'like', '%'.$search.'%');
        });
    }
    if($request->get('price_min'))
    {
        $scripts = $scripts->where('price', '>=', $request->get('price_min'));
    }
    if($request->get('price_max'))
    {
        $scripts = $scripts->where('price', '<=', $request->get('price_max'));
    }
    if($request->get('game_id'))
    {
        $scripts = $scripts->where('game_id', '=', $request->get('game_id'));
    }
    if($request->get('category_id'))
    {
        $scripts = $scripts->where('category_id', '=', $request->get('category_id'));
    }
    switch($request->get('added'))
    {
        case 'year';
            $scripts = $scripts->whereYear('created_at', '>=', date("Y", strtotime("-1 year")));
            $scripts = $scripts->whereYear('created_at', '<=', date("Y", strtotime("-1 year")));
            break;
        case 'month':
            $scripts = $scripts->whereDate('created_at', '>=', date("Y-m-d", strtotime("first day of previous month")));
            $scripts = $scripts->whereDate('created_at', '<=', date("Y-m-d", strtotime("last day of previous month")));
            break;
        case 'week':
            $scripts = $scripts->whereDate('created_at', '>=', date("Y-m-d", strtotime("last week")));
            $scripts = $scripts->whereDate('created_at', '<=', date("Y-m-d", strtotime("last week +6days")));
            break;
        case 'day':
            $scripts = $scripts->whereDate('created_at', '>=', date("Y-m-d", strtotime("-7 days")));
            $scripts = $scripts->whereDate('created_at', '<=', date("Y-m-d", strtotime("now")));
            break;
    }
    $p = 15;
    switch($request->get('sort'))
    {
        case 'price_low':
            $scripts = $scripts->orderBy('price', 'asc')->paginate($p);
            $links = $scripts;
            break;
        case 'price_high':
            $scripts = $scripts->orderBy('price', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'newest_items';
            $scripts = $scripts->orderBy('created_at', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'featured':
            $scripts = $scripts->orderBy('view', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'best_rated':
            $links = $scripts->with('stars')->paginate($p);
            $scripts = $links->sortByDesc(function($script)
            {
                return $script->stars->avg('stars');
            });
            break;
        case 'best_sellers':
            $links = $scripts->with('purchases')->paginate($p);
            $scripts = $links->sortByDesc(function($script)
            {
                return $script->purchases->count();
            });
            break;
        case 'recently_updated':
            $links = $scripts->with('versions')->paginate($p);
            $scripts = $links->sortByDesc(function($script)
            {
                return $script->versions->first()->created_at;
            });
            break;
        default:
            $scripts = $scripts->orderBy('created_at', 'desc')->paginate($p);
            $links = $scripts;
            break;
    }   
    return view('laravel-authentication-acl::client.scripts.index')->with(
        [
            'scripts' => $scripts,
            'links' => $links
        ]
    );
}

嗨,我一整天都在努力让这个分页工作。此处的示例:https://sourcemod.market/scripts?search=&game_id=&category_id=&sort=best_rated&added=&price_min=0&price_max=100

正如你所看到的,我在第一页上得到了3个有评级的产品,我需要在第二页上得到其他产品。。

我尝试了很多东西,WhereHas,Eager loading,With(''(和在发布之前搜索答案。

好的,我做了一个替代

 /**
 * Route : scripts.list
 *
 * @param  Request  $request
 * @return view
 */
public function index(Request $request)
{   
    $scripts = ScriptModel::select(
        '*', 
        DB::raw('(select avg(stars) from `scripts_stars` where `scripts_stars`.`script_id` = `scripts`.`id`) as rating'),
        DB::raw('(select count(*) from `scripts_purchases` where `scripts_purchases`.`script_id` = `scripts`.`id`) as purchases'),
        DB::raw('(select created_at from `scripts_versions` where `scripts_versions`.`script_id` = `scripts`.`id` LIMIT 1) as updated')
    );
    if($request->get('search'))
    {
        $search = $request->get('search');
        $scripts = $scripts->where(function ($query) use ($search) 
        {
            return $query->where('title', 'like', '%'.$search.'%')->orWhere('description', 'like', '%'.$search.'%');
        });
    }
    if($request->get('price_min'))
    {
        $scripts = $scripts->where('price', '>=', $request->get('price_min'));
    }
    if($request->get('price_max'))
    {
        $scripts = $scripts->where('price', '<=', $request->get('price_max'));
    }
    if($request->get('game_id'))
    {
        $scripts = $scripts->where('game_id', '=', $request->get('game_id'));
    }
    if($request->get('category_id'))
    {
        $scripts = $scripts->where('category_id', '=', $request->get('category_id'));
    }
    switch($request->get('added'))
    {
        case 'year';
            $scripts = $scripts->whereYear('created_at', '>=', date("Y", strtotime("-1 year")));
            $scripts = $scripts->whereYear('created_at', '<=', date("Y", strtotime("-1 year")));
            break;
        case 'month':
            $scripts = $scripts->whereDate('created_at', '>=', date("Y-m-d", strtotime("first day of previous month")));
            $scripts = $scripts->whereDate('created_at', '<=', date("Y-m-d", strtotime("last day of previous month")));
            break;
        case 'week':
            $scripts = $scripts->whereDate('created_at', '>=', date("Y-m-d", strtotime("last week")));
            $scripts = $scripts->whereDate('created_at', '<=', date("Y-m-d", strtotime("last week +6days")));
            break;
        case 'day':
            $scripts = $scripts->whereDate('created_at', '>=', date("Y-m-d", strtotime("-7 days")));
            $scripts = $scripts->whereDate('created_at', '<=', date("Y-m-d", strtotime("now")));
            break;
    }
    $p = 15;
    switch($request->get('sort'))
    {
        case 'price_low':
            $scripts = $scripts->orderBy('price', 'asc')->paginate($p);
            $links = $scripts;
            break;
        case 'price_high':
            $scripts = $scripts->orderBy('price', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'newest_items';
            $scripts = $scripts->orderBy('created_at', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'featured':
            $scripts = $scripts->orderBy('view', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'best_rated':
            $scripts = $scripts->orderBy('rating', 'desc')->orderBy('created_at', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'best_sellers':
            $scripts = $scripts->orderBy('purchases', 'desc')->orderBy('created_at', 'desc')->paginate($p);
            $links = $scripts;
            break;
        case 'recently_updated':
            $scripts = $scripts->orderBy('updated', 'desc')->paginate($p);
            $links = $scripts;
            break;
        default:
            $scripts = $scripts->orderBy('created_at', 'desc')->paginate($p);
            $links = $scripts;
            break;
    }   
    return view('laravel-authentication-acl::client.scripts.index')->with(
        [
            'scripts' => $scripts,
            'links' => $links
        ]
    );
}

如果有人有打火机:(请告诉我