我想使用我的show controller
为我的搜索产品项目视图添加分页这是我的控制器
public function show($id)
{
$categories = Item::all();
$products = Item::find($id)->products->paginate(3);
return view('category.index', compact('categories', 'products'));
}
这是我的观点
@extends('layout.front')
@section('page')
<div class="row" id="portfolio">
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-3" style="margin-top:20px;">
<nav class="main-nav">
<ul class="main-nav-ul">
<li style="background-color: #343A40; color: #ffffff; font-weight: 600;" id="sidebar-header"><a>Product List</a></li>
<li><a href="{{ url('/computer') }}" style="font-weight: 600;">Computer<span></span></a>
<ul>
@if(!empty($categories))
@forelse($categories as $category)
<li><a href="{{ route('category.show', $category->id)}}">{{ $category->name }}</a></li>
@empty
<li>No data found</li>
@endforelse
@endif
</ul>
</li>
<li><a href="#" style="font-weight: 600;">CCTV</a></li>
<li><a href="#" style="font-weight: 600;">Gaming</a></li>
</ul>
</nav>
</div>
<!-- /.col-lg-3 -->
<div class="col-lg-9">
<div class="row" style="margin-top:20px;">
@if(!empty($products))
@foreach($products as $key=>$product)
<div class="col-md-4 col-sm-6 portfolio-item" id="items">
<div class="card h-100">
<a href="#"><img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image"></a>
<div class="card-body">
<h4 class="card-title">
<a href="#">{{ $product->category_name }}<br />{{ $product->item_name}}</a>
</h4>
<p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p>
<h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4>
{{--@endforelse--}}
</div>
</div>
</div>
@endforeach
@endif
</div>
<!-- /.row -->
</div>
<!-- /.col-lg-9 -->
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
<div class="col-lg-12">{!! $products->links() !!}</div>
@endsection
我使用了laravel默认分页并将我的引导分页自定义为laravel。
但是在我的show函数中,控制器分页不起作用。如何修复搜索产品的分页。
我使用了链接方法。但它并不适用于这种观点。
<div class="col-lg-12">{!! $comproducts->links() !!}</div>
答案 0 :(得分:0)
您没有在代码中的任何位置使用分页。请查看有关如何添加分页的文档。在进行db调用时需要使用paginate
函数,然后使用paginator链接。
答案 1 :(得分:0)
{{$products->render()}}
将它放在foreach循环的末尾。
答案 2 :(得分:0)
嗨,请在这里找到分页代码
表:股票PK:id,变量:名称
StockController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Controllers\Controller as Controller;
class StockController extends Controller {
public function usaPage() {
$stocks = DB::table('stocks')->orderBy('name', 'ASC')->paginate(50);
return view('stock.usa', ['stocks' => $stocks]);
}
?>
usa.blade.php
@section('content')
<div class="row">
<div class="col-md-12">
<table class="table">
<tr>
<th scope="col">Company Name</th>
<th scope="col">ID</th>
</tr>
@foreach ($stocks as $stock)
<tr><td> {{ $stock->name }}</a></td><td> {{ $stock->id }}</td></tr>
@endforeach
</table>
</div>
</div>
@endsection