3

I have a main template file and it's called by different controllers and different views. In footer the list of categories display so how can i pass data from all controller`s method without writing code in all functions and pass data?

  • Possible duplicate of [How to pass data to all views in Laravel 5?](https://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5) – fubar Jun 18 '18 at 05:30

1 Answers1

5

get categories in app/Providers/AppServiceProvider.php boot method

namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

use Illuminate\Support\Facades\Auth;
use DB;


class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
         View::share('key', 'value');
         Schema::defaultStringLength(191);

        $categories=DB::table('categories')->get();
        View::share('categories',$categories);  

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

now you can access categories in all views and controllers in $categories variable

in your footer :

@foreach($categories as $category)
   <p>{{$category->name}}</p>
@endforeach
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71