简体   繁体   English

是否可以在Blade View(Laravel)中检索数据库记录?

[英]Is it possible to retrieve database records inside a Blade View (Laravel)?

I know database work shouldn't be done inside the view, but unless there's another way, I think I need to do it. 我知道数据库工作不应该在视图中完成,但除非有另一种方式,我认为我需要这样做。

In my web application, I use the same sidebar for every web page and I want to display records from a table in the sidebar. 在我的Web应用程序中,我对每个网页使用相同的侧边栏,我想显示侧边栏中表格的记录。 I don't want to have to retrieve those records in EVERY controller method in my application (as there is a lot). 我不想在我的应用程序中的每个控制器方法中检索那些记录(因为有很多)。

Is it possible to do this inside a blade view? 是否可以在刀片视图中执行此操作? Or is there another way I should do it? 或者我还有另一种方法吗?

You can attach controllers to views! 您可以将控制器附加到视图! This means anytime the view is called, the underlying controller is also called. 这意味着只要调用视图,就会调用底层控制器。 This is called a View Composer 这称为View Composer

使用时应考虑视图作曲家https://laravel.com/docs/5.4/views#view-composers

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

相关问题 如何使用Laravel中的刀片从数据库检索图像并将其显示在视图上 - How to retrieve image from database and display it on view using blade in laravel Laravel:是否可以将刀片视图加载到jQuery模式中? - Laravel: Is it possible to load blade view into a jquery modal? 是否可以在刀片视图中使用groupBy在laravel中进行分页? - Is it possible to do pagination in laravel with groupBy in blade view? Laravel在刀片视图中从数据库获取记录 - Laravel get record from database in blade view Laravel @如果为True,则从SQL隐藏在刀片视图记录中 - Laravel @ hide in blade view records from SQL if True 如何从数据库到laravel刀片检索json数据 - How to retrieve json data from database to laravel blade 在另一个视图中包含一个带有变量的视图(Laravel 5.6-Blade) - Include a view with a variable inside another view (Laravel's 5.6 - Blade) Laravel 5-检索数据库中每个类别的前4条记录 - Laravel 5 - Retrieve the first 4 records for each category in database 是否可以在Laravel(4)刀片模板中获取视图/布局名称 - Is it possible to get the view / layout name in a Laravel (4) blade template 是否可以在 laravel 中返回具有两个变量的视图(使用刀片)? - Is it possible to return a view (using blade) with two variables in laravel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM