php - 如何在 Laravel 中显示通知?

标签 php mysql laravel notifications

我遇到了一个奇怪的问题。 我有两个通知类 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:

<!-- Nav Item - Alerts -->
                    <li class="nav-item dropdown no-arrow mx-1">
                        <a class="nav-link dropdown-toggle" href="#" id="alertsDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Notifications<i class="fas fa-bell fa-fw"></i>

                            @if(Auth::user()->role_id === 1)
                                <!-- Counter - Alerts -->
                                <span class="badge badge-danger badge-counter">{{ $jobSeekerProfile->unreadNotifications->where('type', 'App\Notifications\InterviewRequestReceived')->count() > 0 ? $jobSeekerProfile->unreadNotifications->count() : '' }}</span>
                            @endif

                            @if(Auth::user()->role_id === 2)
                                <!-- Counter - Alerts -->
                                <span class="badge badge-danger badge-counter">{{ Auth::user()->unreadNotifications->where('type', 'App\Notifications\SendJobSeekerResume')->count() }}</span>
                            @endif
                        </a>
                        <!-- Dropdown - Alerts -->
                        <div class="dropdown-list dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="alertsDropdown">
                            <h6 class="dropdown-header">
                                Notifications
                            </h6>

                            @if(Auth::user()->role_id === 1)
                                @foreach($jobSeekerProfile->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

                            @if(Auth::user()->role_id === 2)
                                @foreach(Auth::user()->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

                            <a class="dropdown-item text-center small text-gray-500" href="#">Show All Alerts</a>
                        </div>
                    </li>

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

这是我的通知表的屏幕截图: enter image description here 当我使用 user_id 12(同时 role_id 设置为 2)登录时,它应该显示所有 notification_id 设置为 12 的通知。

我死了,扔掉了我的两个模型,看看通知是否在那里, 雇主文件和jobSeekerProfile模型像这样,我可以看到我的雇主文件模型中没有通知,它只是一个空数组。以下是屏幕截图。

dd($employerProfile->notifications);

enter image description here

dd($jobSeekerProfile->notifications);

enter image description here

EmployerProfile.php 模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class EmployerProfile extends Model
{
    use Notifiable;

    protected $fillable = [

        'user_id',
        'company_name',
        'company_phone',
        'immediate_contact',
        'email',
        'company_address',
        'number_of_employees'

    ];

    public function user(){

        return $this->belongsTo('App\User');

    }

}

JobSeekerProfile.php 模型:

<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class JobSeekerProfile extends Model
{
    use Notifiable;

    protected $fillable = [

        'user_id',
        'photo_id',
        'resume_id',
        'video_one_id',
        'video_two_id',
        'video_three_id',
        'first_name',
        'last_name',
        'email',
        'date_of_birth',
        'full_or_part_time',
        'experience',
        'additional_skills',
        'file'

    ];

    public function user(){

        return $this->belongsTo('App\User');

    }

    public function resume(){

        return $this->belongsTo('App\Resume');

    }

    public function video(){

        return $this->belongsTo('App\Video');

    }

}

有人可以帮忙吗?

最佳答案

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

根据您的代码和数据库结构, 假设登录用户 ID 为 3,employer_profile_user_id 为 12

$user->unreadNotificationsnotifications 表中获取记录,其中 notifying_typeApp\Usernotific_id3

$user->employerProfile->unreadNotificationsnotifications 表中获取记录,其中 notifying_typeApp\EmployerProfile并且 notificable_id12

所以试试这个计数

@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

关于php - 如何在 Laravel 中显示通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60687573/

相关文章:

php - 我应该在 Laravel 中使用 EAV 模型吗?

php - foreach 相当于 jquery 中的 php?

PHP 在类函数中使用 $_SESSION 变量作为默认值

c# - 标记到 c# 和 SQL?

laravel - 如何在用户注册时在 Laravel 中自动设置随机字符串 ID

php - 将复选框值保存到数据库中

php - MySQL 与 php,显示最近的 50 个

PHP 环境变量

java - Mysql表中的URL可以是超链接吗?

mysql - 多个 Insert 语句插入 MySQL 案例