В моем Laravel -5.8 я пытаюсь использовать ViewComposers, чтобы я мог отображать данные в макетах \ header
App \ http \ View \ Composers \ Уведомления Composer
<?php
namespace App\http\View\Composers;
use App\Models\Notification\UserNotification;
use Illuminate\View\View;
use Illuminate\Support\Facades\Auth;
class NotificationsComposer
{
public function compose(View $view)
{
$userCompany = Auth::user()->company_id;
$userID = Auth::user()->id;
$countUnreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->count();
$unreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->take(5)->get();
$allNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->get();
return $view->with([
'countUnreadNotifications' => $countUnreadNotifications,
'unreadNotifications ' => $unreadNotifications,
'allNotifications ' => $allNotifications
]);
}
AppServiceProvider
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\Models\Notification\UserNotification;
use App\Http\View\Composers\NotificationsComposer;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer(['layouts.header'], NotificationsComposer::class);
}
}
layouts\header.blade
<span class="dropdown-item dropdown-header">You have {{ $countUnreadNotifications }} unread notifications</span>
Когда я вошел в систему, я получил эту ошибку:
Неопределенная переменная: countUnreadNotifications (Представление: C: \ xampp \ htdocs \ resources \ views \ layouts \ header.blade. php)
Как мне решить эту проблему?