2016-07-30 106 views
0
/** 
* 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=100Laravel Eloquent分页

正如你所看到的第一页我3种产品的评价,我需要去第二页上让别人..

我尝试很多东西,WhereHas,预先加载在发布之前使用('')和搜索答案。

回答

0

好吧,我做了一个替代

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

如果有人有一些较轻的:)请告诉我