我有一个主模板文件,它由不同的控制器和不同的视图调用。在页脚中显示类别列表,以便如何在不在所有函数中编写代码并传递数据的情况下从所有控制器方法传递数据?
答案 0 :(得分:2)
获取 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()
{
//
}
}
现在您可以访问$categories
变量
:
@foreach($categories as $category)
<p>{{$category->name}}</p>
@endforeach