【问题标题】:Laravel Eloquent PaginationLaravel 雄辩的分页
【发布时间】:2016-07-31 03:36:06
【问题描述】:
 /**
 * 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('') 和搜索答案。

【问题讨论】:

    标签: php laravel pagination eloquent


    【解决方案1】:

    好的,我换了一个

     /**
     * 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
            ]
        );
    }
    

    如果有人有更轻的东西:) 请告诉我

    【讨论】:

      猜你喜欢
      • 2018-02-19
      • 1970-01-01
      • 2020-01-14
      • 2015-01-02
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      相关资源
      最近更新 更多