【问题标题】:Method links do not exist - Laravel 5.5方法链接不存在 - Laravel 5.5
【发布时间】:2017-12-09 14:29:01
【问题描述】:

我收到以下错误:

方法链接不存在

我也尝试了渲染函数,它给了我相同的结果。

这是我的代码:

public function welcome() {
    $products = DB::table('products')->paginate(12);
    return view('welcome', compact('products', $products));
}

当我调用{{ $products->links() }} 时,它会显示错误,但如果我删除{{ $products->links() }} 我可以看到结果。

这是我的看法:

    @foreach($products as $product)
    <div class="col-sm-6 col-md-4 col-lg-3">
        <!-- Product item -->
        <div class="product-item hover-img">
            <a href="product_detail.html" class="product-img">
                <img src="images/default.png" alt="image">
            </a>
            <div class="product-caption">
                <h4 class="product-name"><a href="#">{!! $product->name !!}</a></h4>
                <div class="product-price-group">
                    <span class="product-price">{!! $product->price !!} KM</span>
                </div>
            </div>
            <div class="absolute-caption">
                <form action="{!! route('store') !!}" method="POST">
                    {!! csrf_field() !!}
                    <input type="hidden" name="id" value="{{ $product->id }}">
                    <input type="hidden" name="name" value="{{ $product->name }}">
                    <input type="hidden" name="price" value="{{ $product->price }}">
                    <input type="submit" class="btn btn-success btn-lg" value="Dodaj u korpu">
                </form>
            </div>
        </div>
    </div>
    @endforeach

   {{ $products->links() }}

【问题讨论】:

  • 你需要使用foreach来获取你视图中$products的所有数据
  • 也可以这样返回 return view('welcome')->with('products',$products);
  • 我正在使用 foreach 循环它只是我没有在这里发布它
  • 好的,那么问题出在哪里。它是否由于分页而进入控制器?您可以删除分页,然后检查它是否有效。
  • 如果我删除分页,我会在一页中得到所有结果

标签: php laravel-5 eloquent laravel-5.5 laravel-pagination


【解决方案1】:

您应该使用Eloquent 而不是查询生成器。

用途:

$products = \App\Product::paginate(12);

而不是

$products = DB::table('products')->paginate(12);

让它工作。

(当然需要先创建Product模型,扩展Eloquent模型)

【讨论】:

  • 我再次得到相同的结果
  • @GoGo 试试你也用compact('products')而不是compact('products', $products)
  • 试过了,我得到了同样的结果
  • @GoGo 你确定你没有覆盖视图中的变量吗?请展示你的观点
  • @GoGo 您确定您使用的是有效视图吗?因为看起来你在控制器中使用了get 方法而不是paginate
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 2018-07-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2018-04-21
相关资源
最近更新 更多