简体   繁体   English

每个控制器上的laravel 5负载模型

[英]laravel 5 load model on every controller

I am learning laravel coming from a codeigniter background. 我正在学习来自Codeigniter背景的laravel。 I have an object I need to get from a model to be used in the navigation bar. 我有一个需要从模型中获取的对象,以用于导航栏中。 The call I'm making is this. 我打的电话是这个。

$users = \App\Models\User::all();

I know I can do this on every controller 我知道我可以在每个控制器上执行此操作

public function index()
{       
    $users = \App\Models\User::all();

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

But I'm trying to get in good habits. 但是我正在努力养成良好的习惯。 What is the best practice for retrieving this data on every page in laravel 5? 在laravel 5的每个页面上检索此数据的最佳实践是什么?

Use the view helper function to share data with every view: view()->share('data', [1, 2, 3]); 使用view助手功能可与每个视图共享数据: view()->share('data', [1, 2, 3]);

You can do that in the boot method of the AppServiceProvider . 您可以在AppServiceProviderboot方法中执行此操作。 For more information look here . 有关更多信息,请参见此处

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

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