Laravel 防止在每个视图生成器中使用同一查询两次

Laravel 防止在每个视图生成器中使用同一查询两次,laravel,laravel-5,laravel-5.3,Laravel,Laravel 5,Laravel 5.3,我的AppServiceProvider类中有两个视图生成器,如下所示: class AppServiceProvider extends ServiceProvider { public function boot() { View::composer('left', function ($view) { if (Auth::check()) { $user = Au

我的AppServiceProvider类中有两个视图生成器,如下所示:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        View::composer('left', function ($view)
        {
            if (Auth::check())
            {
                $user = Auth::user();

                // Gets a list of the people the user is following
                $usersFollowing = Following::where('user_id', $user->id)
                    ->get();

                // More queries here

                View::share('usersFollowing', $usersFollowing);
            }
        });

        View::composer('right', function ($view)
        {
            if (Auth::check())
            {
                $user = Auth::user();

                // Gets a list of the people the user is following
                $usersFollowing = Following::where('user_id', $user->id)
                    ->get();

                // More queries here

                View::share('usersFollowing', $usersFollowing);
            }
        });
    }
}
如您所见,两个编写器都请求相同的查询数据$usersFollowing。这些布局left.blade.php和right.blade.php都通过将它们包含在基本布局中而在我的所有页面上被调用

问题是,页面在一次页面加载中请求$users两次。它对left.blade.php调用一次查询,对right.blade.php调用一次查询

我还将调用Auth::user两次,每个编写器调用一次


如何防止对同一请求调用两次这些查询,而只调用一次?

我认为将查询移到方法的顶部并在两个视图生成器中使用它们很简单。这样,您的查询将只运行一次。 以下是我建议的方法

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $user = Auth::user();

        // Gets a list of the people the user is following
        $usersFollowing = Following::where('user_id', $user->id)
                    ->get();
        // You can use `use` keyword to access external variables inside callback function. 
        //Both of these variables will be accessible inside callback
        View::composer('left', function ($view) use ($usersFollowing,$user)
        {
            if (Auth::check())
            {

                // More queries here

                View::share('usersFollowing', $usersFollowing);
            }
        });

        View::composer('right', function ($view) use ($usersFollowing,$user)
        {
            if (Auth::check())
            {

                // More queries here

                View::share('usersFollowing', $usersFollowing);
            }
        });
    }
}

我希望这会有所帮助,并且您可以将此方法推广到需要此类功能的任何其他情况。

我认为将查询移到方法的顶部并在两个视图生成器中使用它们很简单。这样,您的查询将只运行一次。 以下是我建议的方法

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $user = Auth::user();

        // Gets a list of the people the user is following
        $usersFollowing = Following::where('user_id', $user->id)
                    ->get();
        // You can use `use` keyword to access external variables inside callback function. 
        //Both of these variables will be accessible inside callback
        View::composer('left', function ($view) use ($usersFollowing,$user)
        {
            if (Auth::check())
            {

                // More queries here

                View::share('usersFollowing', $usersFollowing);
            }
        });

        View::composer('right', function ($view) use ($usersFollowing,$user)
        {
            if (Auth::check())
            {

                // More queries here

                View::share('usersFollowing', $usersFollowing);
            }
        });
    }
}
我希望这会有所帮助,并且您可以将此方法推广到需要此类功能的任何其他情况