这是我写的资料。
app \ Providers \ AppServiceProvider.php
<div class="Main">
<div class="Relative">
<div class="Background"></div>
<div class="Absolute">
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
</div>
</div>
<footer>With absolute positioning, an element can be placed anywhere on a page. The heading below is placed 100px from the left of the page and 150px from the top of the page.</footer>
</div>
发生以下错误:
“试图获取非对象的属性'user_id'”
当我在其他地方使用namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::share('user_id', Auth::user()->user_id);
}
}
时,它显示正确。但是不在这里。是什么原因?
答案 0 :(得分:1)
您的用户表中是否有名为“ user_id”的列?或者您是说用户表中的“ id”列?如果您是说用户表ID列将您的代码更新为以下代码。
IntelHexFormatReader
答案 1 :(得分:0)
我通过下面的链接找到了原因。
Laravel - How to get current user in AppServiceProvider
我按如下所示修改了源代码,并且有效。
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('*', function ($view)
{
View::share('user_id', Auth::user()->user_id);
}
}
}