1

I was working with Laravel and I am trying to paginate my table of books. I got this error "Call to a member function links() on array in Laravel". It may be as the duplicate's error, but I still can't figure out how to solve my thing.

BookController fiddle:

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 fiddle

<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() }}
Irfan
  • 95
  • 2
  • 11
  • Possible duplicate of [Laravel Call to a member function toArray() on array Error](http://stackoverflow.com/questions/26536518/laravel-call-to-a-member-function-toarray-on-array-error) – Marcin Orlowski Apr 28 '17 at 12:38
  • Are $books isa an array or object? – Muhammad Rizwan Apr 28 '17 at 12:39
  • 1
    Try this $books=DB::table('books')->paginate(3); instead of $books = Book::simplePaginate(3)->all(); – Nil Apr 28 '17 at 12:42
  • nothing's working – Irfan Apr 28 '17 at 12:51
  • @MuhammadRizwan It should be an array. – Irfan Apr 28 '17 at 12:59
  • @Irfan : why it should be an array, I am just asking you is it array or object..If it is array links() method doesn't work on array, it must be object, that's why you are getting error – Muhammad Rizwan Apr 28 '17 at 13:03
  • I also have gotten the same problem. Getting error like, "Call to a member function links() on array". I can't use the collection, need to use an array. What's the solution @Irfan – Ashish Jul 12 '18 at 00:00

1 Answers1

2

Just remove the ->all() method from the $books variable.

$books = Book::paginate(10);

paginate() function considers taking all content from the table, which is taken here by Book model

Irfan
  • 95
  • 2
  • 11