Shared data between certain views in Laravel

Viewed 37

I want to share data on all my frontend views but not on the backend or dashboard pages.

  view()->composer('*', function ($view)
  {
      // Some eloquent queries to return data that would only be used on frontend pages/views
    view()->share('data', $data);
  }

The problem with this approach is that it runs on every view render, is there a way i can limit it to only frontend views?

As an alternative i am thinking to create separate routes group and with some middleware to share the data between route group something like below

Route::group(['middleware' => ['frontend']], function () { //Frontend Routes });

// frontend Middleware

public function handle(Request $request, Closure $next)
    {
        View::share(
            'data',  'data'
        );

        return $next($request);
    }

Can someone please guide me what is the best approach to handle this?

0 Answers
Related