Ryan Sacks
Ryan Sacks

Reputation: 524

How to display notifications in Laravel?

I'm having a weird issue. I have two notification classes InterviewRequestReceived.php and SendJobSeekerResume.php. I'm trying to display the total number of notifications using count() next to a little bell icon and then a message related to the respective notification class. The messages are simple, they are located in /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>

in my admin file I have 2 roles, depending on if the user is logged in as a jobseeker or employer. 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>

Both items with role_id === 1 are working, I get the count and the item with the message, but the items where role_id === 2 I get count 0 and no item with message, it is very strange. Of course I'm viewing and testing with 2 accounts where I either have role_id set to 1 or role_id set to 2.

Here is a screenshot of my Notifications Table: enter image description here When I login with the user_id 12 (also role_id is set to 2) it should display all of the notifications where notifiable_id is set to 12.

I die and dumped my two models to see if the notifications are there, employerProfile and jobSeekerProfile models like this, and I can see that there are no notifications in my employerProfile model, it is just an empty array. Below are screenshots.

dd($employerProfile->notifications);

enter image description here

dd($jobSeekerProfile->notifications);

enter image description here

EmployerProfile.php model:

<?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 model:

<?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');

    }

}

Can anyone help?

Upvotes: 2

Views: 8707

Answers (1)

Evol Rof
Evol Rof

Reputation: 2801

You are getting notifications from wrong Model.

According to your code and database structure, assume login user id is 3,employer_profile_user_id is 12

$user->unreadNotifications get records from notifications table where notifiable_type is App\User and notifiable_id is 3;

$user->employerProfile->unreadNotifications get records from notifications table where notifiable_type is App\EmployerProfile and notifiable_id is 12;

so try this for count

@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

and this for detail

@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

Upvotes: 2

Related Questions