简体   繁体   English

Laravel的雄辩功能不起作用

[英]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. 我是laravel的新手,我想使用它的雄辩功能,但出现错误。

in routes.php 在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 在我的contact.blade.php中

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

But it doesn't see $books variable. 但是它没有看到$ books变量。 Please help me. 请帮我。 How can I fix it? 我该如何解决? When I say that 当我说那

return $books; 返回$ books; it works. 有用。 I can get values from database but eloquent doesn't work. 我可以从数据库中获取值,但是雄辩是行不通的。

Have you checked the docs of Laravel? 您是否查看过Laravel的文档? What version are u using? 您正在使用什么版本? Assuming you are using Laravel 5.2 can you try to do this? 假设您正在使用Laravel 5.2,可以尝试这样做吗?

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) 您也可以将雄辩的集合转换为数组或json(我通常在使用ajax时使用它)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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