如何在 Laravel 中显示通知?
How to display notifications in Laravel?
我遇到了一个奇怪的问题。
我有两个通知 classes InterviewRequestReceived.php 和 SendJobSeekerResume.php。
我正在尝试使用小铃铛图标旁边的 count()
显示通知总数,然后显示与相应通知相关的消息 class。消息很简单,它们位于 /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。
这是我的通知的屏幕截图 Table:
当我使用 user_id 12(也 role_id 设置为 2)登录时,它应该显示 notifiable_id 设置为 12 的所有通知。
我死了,把我的两个模型都扔了,看看有没有通知,
employerProfile 和 jobSeekerProfile 模型是这样的,我可以看到我的 employerProfile 模型中没有通知,它只是一个空数组。以下是屏幕截图。
dd($employerProfile->notifications);
dd($jobSeekerProfile->notifications);
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->unreadNotifications
从 notifications
table 获取记录,其中 notifiable_type
是 App\User
而 notifiable_id
是 3
;
$user->employerProfile->unreadNotifications
从 notifications
table 获取记录,其中 notifiable_type
是 App\EmployerProfile
而 notifiable_id
是 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
我遇到了一个奇怪的问题。
我有两个通知 classes InterviewRequestReceived.php 和 SendJobSeekerResume.php。
我正在尝试使用小铃铛图标旁边的 count()
显示通知总数,然后显示与相应通知相关的消息 class。消息很简单,它们位于 /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。
这是我的通知的屏幕截图 Table:
我死了,把我的两个模型都扔了,看看有没有通知, employerProfile 和 jobSeekerProfile 模型是这样的,我可以看到我的 employerProfile 模型中没有通知,它只是一个空数组。以下是屏幕截图。
dd($employerProfile->notifications);
dd($jobSeekerProfile->notifications);
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->unreadNotifications
从 notifications
table 获取记录,其中 notifiable_type
是 App\User
而 notifiable_id
是 3
;
$user->employerProfile->unreadNotifications
从 notifications
table 获取记录,其中 notifiable_type
是 App\EmployerProfile
而 notifiable_id
是 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