为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Laravel 中显示通知?

如何在 Laravel 中显示通知?

PHP
杨__羊羊 2022-10-28 16:27:57
我有一个奇怪的问题。我有两个通知类 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。这是我的通知表的屏幕截图:当我使用 user_id 12(也将 role_id 设置为 2)登录时,它应该显示所有 notifiable_id 设置为 12 的通知。我死掉了我的两个模型,看看是否有通知,像这样的employeesProfile 和jobSeekerProfile 模型,我可以看到我的employeesProfile 模型中没有通知,它只是一个空数组。下面是截图。dd($employerProfile->notifications);
查看完整描述

1 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

您收到来自错误模型的通知。


根据您的代码和数据库结构,假设登录用户 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)

      <!-- Counter - Alerts -->

      <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)

       @foreach(Auth::user()->employerProfile->unreadNotifications as $notification)

       <a class="dropdown-item d-flex align-items-center" href="#">

            @include('layouts.partials.notification.'. Str::snake(class_basename($notification->type)))

                                    </a>

       @endforeach

@endif


查看完整回答
反对 回复 2022-10-28
  • 1 回答
  • 0 关注
  • 127 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号