Gizem Abalı Gizem Abalı - 6 months ago 8x
PHP Question

Eloquent Feature of Laravel Doesn't work

I'm new to laravel and I want to use eloquent feature of it but I got an error.

in routes.php

Route::get('/contact', function(){

$books = Books::all();

return View::make('')->with('', $books);

and in my contact.blade.php
I wrote

@foreach ($books as $book)
<p> {{ $book -> title}}</p>

But it doesn't see $books variable. Please help me. How can I fix it? When I say that

return $books; it works. I can get values from database but eloquent doesn't work.


Have you checked the docs of Laravel? What version are u using? Assuming you are using Laravel 5.2 can you try to do this?

return view('', ['books' => $books]);
return view('')->with('books', $books);

You can iterate a collection in a blade template. Also you can transform that eloquent collection to an array or a json (I usually use this when I use ajax)