drecy22400 2018-11-28 04:42 采纳率: 0%
浏览 32

分页追加不适用于laravel

I have my search result I also have my paginator in the view but when I click on pages it returns 404 error.

I've tried this:

1- @include('front.partials.pagination', ['paginator' => $products])

2- {{$products->appends(Request::except(['page','_token']))->links() }}

3- {{$products->appends(request()->query())->links() }}

4- $products->appends(['brands' => $brandss]); //in contoller

All of them have the same result 404 Error

Here is my complete function:

public function advancedsearch(Request $request) {
    // dd($request->all());
    $options = Specification::whereHas('subspecifications')->with(['subspecifications' => function($query){
        $query->status('Active');
    }])->get();

    $brandss = Input::has('brands') ? Input::get('brands') : [];
    $suboption = Input::has('suboptions') ? (int)Input::get('suboptions') : [];
    $min_price = Input::has('min_price') ? (int)Input::get('min_price') : null;
    $max_price = Input::has('max_price') ? (int)Input::get('max_price') : null;


    //codes
    if(!empty($request->input('brands')) && !empty($request->input('min_price')) && !empty($request->input('max_price'))){
      $products = Product::
      where('price', '>=', $min_price)
      ->where('price', '<=', $max_price)
      ->whereIn('brand_id', $brandss)
      ->paginate(12);
      $products->appends(['brands' => $brandss, 'suboptions' => $suboption, 'min_price' => $min_price, 'max_price' => $max_price]);
    }


    elseif(!empty($request->input('suboptions')) && !empty($request->input('min_price')) && !empty($request->input('max_price'))){
      $products = DB::table('products')
        ->join('product_subspecification', function ($join) {
          $suboption = Input::has('suboptions') ? Input::get('suboptions') : [];
              $join->on('products.id', '=', 'product_subspecification.product_id')
                   ->where('product_subspecification.subspecification_id', '=', $suboption);
          })
          ->where('products.price', '>=', $min_price)
          ->where('products.price', '<=', $max_price)
        ->paginate(12);
        $products->appends(['brands' => $brandss, 'suboptions' => $suboption, 'min_price' => $min_price, 'max_price' => $max_price]);
    }

    //
    elseif(!empty($request->input('brands')) && !empty($request->input('suboptions'))){
      $products = DB::table('products')
        ->whereIn('products.brand_id', $brandss)
        ->join('product_subspecification', function ($join) {
          $suboption = Input::has('suboptions') ? Input::get('suboptions') : [];
              $join->on('products.id', '=', 'product_subspecification.product_id')
                   ->where('product_subspecification.subspecification_id', '=', $suboption);
          })
        ->paginate(12);
        $products->appends(['brands' => $brandss, 'suboptions' => $suboption]);
    }

    elseif(!empty($request->input('brands')) && !empty($request->input('suboptions')) && !empty($request->input('min_price')) && !empty($request->input('max_price'))){
      $products = DB::table('products')
        ->join('product_subspecification', function ($join) {
          $suboption = Input::has('suboptions') ? Input::get('suboptions') : [];
              $join->on('products.id', '=', 'product_subspecification.product_id')
                   ->where('product_subspecification.subspecification_id', '=', $suboption);
          })
          ->where('products.price', '>=', $min_price)
          ->where('products.price', '<=', $max_price)
          ->whereIn('products.brand_id', $brandss)
        ->paginate(12);
        $products->appends(['brands' => $brandss, 'suboptions' => $suboption, 'min_price' => $min_price, 'max_price' => $max_price]);
    }
    //


    elseif(!empty($request->input('suboptions'))){
        $products = DB::table('products')
        ->join('product_subspecification', function ($join) {
          $suboption = Input::has('suboptions') ? Input::get('suboptions') : [];
              $join->on('products.id', '=', 'product_subspecification.product_id')
                   ->where('product_subspecification.subspecification_id', '=', $suboption);
          })
        ->paginate(12);
        $products->appends(['suboptions' => $suboptions]);
    }

    elseif(!empty($request->input('brands'))){
      $products = DB::table('products')
      ->whereIn('products.brand_id', $brandss)
      ->paginate(12);
      $products->appends(['brands' => $brandss]);
    }

    elseif($request->has('min_price') && $request->has('max_price')){
      $products = DB::table('products')
      ->whereBetween('price', [$min_price, $max_price])
      ->paginate(12);
      $products->appends(['min_price' => $min_price, 'max_price' => $max_price]);
    }


    return view('front.advancesearch', compact('products', 'options'));
}

Any idea?

................................................................................................................................................................................................................

  • 写回答

1条回答 默认 最新

  • dsfs21312 2018-11-28 05:45
    关注

    You are using Eloquent in the first part and query builder in the second part, and appends is an eloquent method, the collection that you receive from query builder is not the same than the one from eloquent Eloquent.. it doesnt have the same methods...

    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果