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?
Asked
Active
Viewed 2,341 times
3
-
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 Answers
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
-
This is called view composers and further details can be found here: https://laravel.com/docs/5.6/views#view-composers – Gertjan Brouwer Jun 18 '18 at 08:09