1
I am trying to access the last "Books" inserted in the bank, and display in the view.
This controller function returns the view with the recovered values from the database.
public function index(){
$books = Book::orderBy('created_at', 'desc')->take(5);
return view('index')->with('books', $books);
}
The view is like this:
@foreach($books as $book)
{{$book->name}}
@endforeach
But nothing is displayed (there is data in the database), using {{dd($books)}}
I have it in return:
Builder {#180 ▼
#query: Builder {#179 ▶}
#model: Book {#168 ▶}
#eagerLoad: []
#macros: []
#onDelete: null
#passthru: array:11 [▶]
#scopes: []
#removedScopes: []
}
I cannot understand why the foreach is not displaying the values, since dd() shows that the object was passed. How can I correctly access the values of the last 5 Books?
try passing the data to the view this way;
return view('index', ['books' => $books]);
– RFL