【问题标题】:How to remove paginate in Laravel?如何在 Laravel 中删除分页?
【发布时间】:2021-02-06 13:09:13
【问题描述】:

我需要删除分页并将所有内容留在一页上,但我这样做只会产生BUG。 我想知道为什么注释代码的sn-p不能去除分页。

if ($minParam || $maxParam) {
  $products = Product::whereHas('sizes', function ($query) use ($minParam, $maxParam) {
    if ($minParam && $maxParam) {
      $query->whereBetween('max_capacity', [$minParam, $maxParam]);
    } elseif ($minParam) {
      $query->where('max_capacity', '>=', $minParam);
    } else {
      $query->where('max_capacity', '<=', $maxParam);
    }
  })
    ->whereHas('solutions', function ($query) use ($solution_id) {
      $query->whereIn('solution_id', $solution_id);
    })
    ->where('active', 1)
    ->orderBy('position', 'ASC')
    ->get();
  //->paginate(16);
} else {
  $products = Product::whereHas('solutions', function ($query) use ($solution_id) {
    $query->whereIn('solution_id', $solution_id);
  })
    ->where('active', 1)
    ->orderBy('position', 'ASC')
    ->get();
  //->paginate(16);
}
return view('solutions.show')->with(compact('solutions', 'solution', 'products', 'ranges'));

} }

用get()替换后的bug

错误异常 (E_ERROR) 方法 Illuminate\Database\Eloquent\Collection::links 不存在。 (查看:/app/server/resources/views/solutions/show.blade.php) 以前的例外 方法 Illuminate\Database\Eloquent\Collection::links 不存在。

【问题讨论】:

  • 你指的是什么bug?您仍在使用-&gt;paginate(),因此可以预期分页。
  • 正是我需要做一个分页,但只是把它拿出来放 get() 是行不通的。
  • 你需要分页,还是不需要分页?您还没有告诉我们您收到的错误消息是什么。这可能会有所帮助。
  • 你得到的错误是什么? )-&gt; if ($minParam || $maxParam) 应该做什么?
  • Collection::links on show.blade.php show 视图仍在使用分页链接。找到它并把它拿出来。问题不在于您在此处显示的代码。

标签: php laravel laravel-5 backend illuminate


【解决方案1】:

尝试在所有代码中将-&gt;paginate(16) 更改为-&gt;get(),如下所示:

    whereHas('sizes', function($query)  use($minParam, $maxParam) {
         if($minParam && $maxParam) {
             $query->whereBetween('max_capacity', [$minParam, $maxParam]);
         } elseif($minParam) {
             $query->where('max_capacity', '>=', $minParam);
         } else {
             $query->where('max_capacity', '<=', $maxParam);
         }
     })->
    
    if ($minParam || $maxParam) {
      $products = Product::whereHas('sizes', function ($query) use ($minParam, $maxParam) {
        if ($minParam && $maxParam) {
          $query->whereBetween('max_capacity', [$minParam, $maxParam]);
        } elseif ($minParam) {
          $query->where('max_capacity', '>=', $minParam);
        } else {
          $query->where('max_capacity', '<=', $maxParam);
        }
      })
        ->whereHas('solutions', function ($query) use ($solution_id) {
          $query->whereIn('solution_id', $solution_id);
        })
        ->where('active', 1)
        ->orderBy('position', 'ASC')
        ->get();
    } else {
      $products = Product::whereHas('solutions', function ($query) use ($solution_id) {
        $query->whereIn('solution_id', $solution_id);
      })
        ->where('active', 1)
        ->orderBy('position', 'ASC')
        ->get();

 /*$products = Product::whereHas('solutions', function ($query) use ($solution_id) {
    $query->whereIn('solution_id', $solution_id);
  })
    ->where('active', 1)
    ->orderBy('position', 'ASC');*/
    
      return view('solutions.show')->with(compact('solutions', 'solution', 'products', 'ranges'));
    
    }

【讨论】:

  • 我在整个代码中将 -> paginate (16) 更改为 -> get () ,但错误仍然存​​在。不要将项目放在一个页面上。
  • @iLuucasD 编辑您的问题并向我们展示您遇到的错误。
猜你喜欢
  • 2023-03-12
  • 2023-04-09
  • 1970-01-01
  • 2023-04-09
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多