我已經嘗試在構造函數中注入Guard守護進程,我嘗試過四處移動。但是當用戶登錄時 - Auth :: check()返回false。Auth :: check在AppServiceProvider中返回false
在其他文件(全局中間件除外)Auth :: check()工作正常。 在中間件 - 移動Auth Check到頂部有助於緩解問題。在這種情況下 - 它不起作用。
附加信息:此應用程序已從4.2升級。以前它使用Confide。
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if(Auth::check())
{
$user = Auth::user();
$messages=Message::where('read',0);
$messages->where(function ($query) use ($user) {
$query->where('to',$user->id)->orwhere('from',$user->id);
});
$message_unread= $messages->count();
$new_notifications= Notification::where('user_id',$user->id)->where('viewed',0)->count();
}
else
{
$message_unread=0;
$new_notifications=8888888;
//其888888用於測試目的。
}
view()->share(([
'message_unread'=>$message_unread,
'new_notifications'=>$new_notifications
]));
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
[Auth class和auth()函數的可能重複不會在Eloquent模型中工作。 (Laravel 5)](http://stackoverflow.com/questions/34835200/auth-class-and-auth-function-doesnt-works-in-eloquent-model-laravel-5) –
我會檢查出來。謝謝。 –
不要刪除此問題 –