dongliao6777 2015-04-28 18:22
浏览 18
已采纳

laravel 5在每个控制器上加载模型

I am learning laravel coming from a codeigniter background. 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?

  • 写回答

1条回答 默认 最新

  • dtmsaqtly798322992 2015-04-28 19:57
    关注

    Use the view helper function to share data with every view: view()->share('data', [1, 2, 3]);

    You can do that in the boot method of the AppServiceProvider. For more information look here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部