Php 是否将全局变量从数据库结果共享到视图?
在我的页眉和页脚中有一些来自数据库的动态导航。所以我需要一个结果,从数据库中的每个网站在拉威尔 在Laravel的文件中,以下是描述的方式 因此我在Php 是否将全局变量从数据库结果共享到视图?,php,laravel,laravel-5,continuous-integration,Php,Laravel,Laravel 5,Continuous Integration,在我的页眉和页脚中有一些来自数据库的动态导航。所以我需要一个结果,从数据库中的每个网站在拉威尔 在Laravel的文件中,以下是描述的方式 因此我在boot()下的AppServiceProvider中创建了查询: 它可以工作,但我使用CI,每次测试都失败 > Illuminate\Foundation\ComposerScripts::postInstall > [ -f /usr/bin/php71 ] && /usr/bin/php71 artisan opti
boot()
下的AppServiceProvider
中创建了查询:
它可以工作,但我使用CI,每次测试都失败
> Illuminate\Foundation\ComposerScripts::postInstall
> [ -f /usr/bin/php71 ] && /usr/bin/php71 artisan optimize || php artisan optimize
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.navigation' doesn't exist (SQL: select * from `navigation`)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.navigation' doesn't exist
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.navigation' doesn't exist
这是因为此时未迁移表
因此,我捕捉到如下异常:
try {
$navigation = \App\Navigation::all();
} catch (\Illuminate\Database\QueryException $e) {
$navigation = [];
}
view()->share('navigation', $navigation);
这是正确的方法还是有更好的方法将数据库结果共享给视图 之所以有此功能,是因为您的数据库尚未迁移 使用
composer
而不是share
,这样当确实需要数据时,而不是在应用程序启动时,将调用闭包(或ViewComposer类)
看医生
try {
$navigation = \App\Navigation::all();
} catch (\Illuminate\Database\QueryException $e) {
$navigation = [];
}
view()->share('navigation', $navigation);