Reputation: 63
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?
Upvotes: 3
Views: 2411
Reputation: 13699
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
Upvotes: 5