如何在 Laravel 中显示通知?

我有一个奇怪的问题。我有两个通知类 InterviewRequestReceived.php 和 SendJobSeekerResume.php。我正在尝试使用小铃铛图标旁边显示通知总数count(),然后显示与相应通知类相关的消息。消息很简单,它们位于 /layouts/partials/notification 中。


1. interview_request_received.blade.php

<div>

    <span class="font-weight-bold">You've been sent an Interview request!</span>

</div>

2. send_job_seeker_resume.blade.php

<div>

    <span class="font-weight-bold">You've been sent a new Job Profile!</span>

</div>

在我的管理文件中,我有 2 个角色,具体取决于用户是作为求职者还是雇主登录。admin.blade.php:

两个项目role_id === 1都在工作,我得到了计数和带有消息的项目,但是role_id === 2我得到计数 0 的项目并且没有带有消息的项目,这很奇怪。当然,我正在查看和测试 2 个帐户,其中我将 role_id 设置为 1 或将 role_id 设置为 2。

这是我的通知表的屏幕截图:

http://img.mukewang.com/635b92bf000181ca09960470.jpg

当我使用 user_id 12(也将 role_id 设置为 2)登录时,它应该显示所有 notifiable_id 设置为 12 的通知。

我死掉了我的两个模型,看看是否有通知,像这样的employeesProfile 和jobSeekerProfile 模型,我可以看到我的employeesProfile 模型中没有通知,它只是一个空数组。下面是截图。

dd($employerProfile->notifications);


杨__羊羊
浏览 128回答 1
1回答

函数式编程

您收到来自错误模型的通知。根据您的代码和数据库结构,假设登录用户 id 为 3,employer_profile_user_id则为 12$user->unreadNotifications从notifications表 where notifiable_typeisApp\User和notifiable_idis中获取记录3;$user->employerProfile->unreadNotifications从notifications表 where notifiable_typeisApp\EmployerProfile和notifiable_idis中获取记录12;所以试试这个计数@if(Auth::user()->role_id === 2)&nbsp; &nbsp; &nbsp; <!-- Counter - Alerts -->&nbsp; &nbsp; &nbsp; <span class="badge badge-danger badge-counter">{{ Auth::user()->employerProfile->unreadNotifications->where('type', 'App\Notifications\SendJobSeekerResume')->count() }}</span>@endif这是细节@if(Auth::user()->role_id === 2)&nbsp; &nbsp; &nbsp; &nbsp;@foreach(Auth::user()->employerProfile->unreadNotifications as $notification)&nbsp; &nbsp; &nbsp; &nbsp;<a class="dropdown-item d-flex align-items-center" href="#">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @include('layouts.partials.notification.'. Str::snake(class_basename($notification->type)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp;@endforeach@endif
打开App,查看更多内容
随时随地看视频慕课网APP