Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michael Fayez's avatar

Call to a member function assignees() on null

What's missing in this code although everything in my mind ok

<?php



namespace App\Listeners\Design;



use App\Notifications\Design\DesignCreatedNotification;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Support\Facades\Notification;

use App\Models\Project;

use App\Models\Design;



class DesignCreatedEmail

{

    public function handle(object $event): void

    {

        $design      = $event->design;

        $project     = $design->project;

        $assignees   = $project->assignees()->get();



        Notification::send(

            $assignees,

            new DesignCreatedNotification($design)

        );

    }

}

why get's me null although there is a 2 assignees in projects

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you're encountering an issue where $project is null, which means that when you try to call $project->assignees(), you're calling a method on a non-object, hence the error "Call to a member function assignees() on null".

Here are a few steps to debug this issue:

  1. Ensure that the $design object actually has an associated project. You can do this by checking if $design->project is not null before trying to access the assignees relationship.

  2. Make sure that the project relationship is correctly defined in the Design model.

  3. Check if the assignees relationship is correctly defined in the Project model.

Here's an updated version of your code with added checks:

<?php

namespace App\Listeners\Design;

use App\Notifications\Design\DesignCreatedNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
use App\Models\Project;
use App\Models\Design;

class DesignCreatedEmail
{
    public function handle(object $event): void
    {
        $design = $event->design;

        // Check if the design has a project associated with it
        if ($design->project) {
            $project = $design->project;

            // Check if the project has assignees
            if ($project->assignees) {
                $assignees = $project->assignees()->get();

                Notification::send(
                    $assignees,
                    new DesignCreatedNotification($design)
                );
            } else {
                // Handle the case where there are no assignees
                // Perhaps log this situation or handle it accordingly
            }
        } else {
            // Handle the case where the design does not have a project
            // Perhaps log this situation or handle it accordingly
        }
    }
}

Make sure that the relationships in your Design and Project models are defined like this:

In your Design model:

public function project()
{
    return $this->belongsTo(Project::class);
}

In your Project model:

public function assignees()
{
    // Assuming 'assignees' is a relationship, for example, a many-to-many relationship
    return $this->belongsToMany(User::class, 'project_user', 'project_id', 'user_id');
}

If after these checks you're still facing issues, you might want to log or dump the $design variable to ensure it's loaded correctly and that the project relationship is indeed populated.

2 likes
kevinbui's avatar

@laryai answer is spot on and should be the best answer. I personally would refactor to this:

$assignees = $event?->design?->project?->assignees;

if ($assignees) {
    Notification::send($assignees, new DesignCreatedNotification($design));
}

Or:

use Illuminate\Database\Eloquent\Collection;

tap($event?->design?->project?->assignees, function (?Collection $assignees) use ($design) {
    if ($assignees) {
        Notification::send($assignees, new DesignCreatedNotification($design));
    }
});

Please or to participate in this conversation.