简体   繁体   中英

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('pages.contact')->with('pages.contact', $books);
});

and in my contact.blade.php I wrote

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

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('pages.contact', ['books' => $books]);
or
return view('pages.contact')->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)

LARAVEL DOCS: https://laravel.com/docs/5.2/views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM