从数据库中获取信息并输出到视图一次|第一次

Get information from database and output to the view just one|first time

我正在尝试从数据库中获取一些信息,根据登录用户过滤该信息,然后 return 将正确的信息添加到视图中,以便根据用户组|角色|权限构建菜单.

这应该只在用户登录应用程序后发生一次。对于我使用的菜单 this package and for the user/groups/permissions I'm using Cartalyst Sentinel

我可以使用 Middleware 但我认为它的目的不是从数据库获取信息并构建一些输出或 View Composers 但我不希望它全部执行调用布局或 header 的时间,这几乎是所有时间,或者我会说的所有时间,或者我可以使用 Service Provider 我不太了解这个最新的。

有了这些信息:在这种情况下你会怎么做?您将如何构建菜单并输出到 layoutsection@header?

这是一个简单的例子

假设您要检索具有角色的用户并缓存结果

public function test() {
//getting authenticated user with roles
$userwithroles = Cache::remember('users-'.Auth::user()->id, 60, function() {
return User::with('roles')->where('id', Auth::user()->id)->get();
});
  return View('home')->with('$data',$userwithroles);
}

现在,查询将仅每小时执行一次,您可以查看 laravel 文档以了解更多技巧和其他有趣的东西

Caching in laravel

你也可以检查这个Video(它laravel 4但是几乎一样)

希望这对您有所帮助