【问题标题】:Call to a member function links() on array in Laravel error在 Laravel 错误中调用数组上的成员函数 links()
【发布时间】:2017-04-28 12:36:14
【问题描述】:

我正在使用 Laravel,我正在尝试对我的表进行分页。我收到此错误“调用 Laravel 中数组上的成员函数 links()”。这可能是副本的错误,但我仍然无法弄清楚如何解决我的问题。

BookController 小提琴:

public function index()
{
    $books = Book::simplePaginate(3)->all();
    $authors = Author::all();
    $genres = Genre::all();
    return view('books.all')->withBooks($books)->withAuthors($authors)->withGenres($genres);
}

books/all.blade.php 小提琴

<table class="table table-hover">
  <tr class="info">
  <td>#</td>
  <td>Name</td>
  <td>Author</td>
  <td><center>Visit</center></td>
  </tr>
  @foreach($books as $book)
  <tr>
    <td width="50px"><img width="50px" height="75px" src="{{ asset('images/' . $book->image) }}"></td>
    <td width="50px">{{ $book->name }}</td>
    <td width="50px">{{ $book->author->name }}</td>
    <td width="50px"><center><a href="{{ url('books', $book->id) }}"><button class="btn btn-success">Visit</button></a></td>
 </tr>
@endforeach
</table> 
{{ $books->links() }}

【问题讨论】:

标签: php laravel laravel-paginate


【解决方案1】:

只需从 $books 变量中删除 ->all() 方法即可。

$books = Book::paginate(10);

paginate() 函数考虑从表中取所有内容,这里是由 Book 模型取的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 2021-09-04
    • 2018-05-19
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    相关资源
    最近更新 更多