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:
-
Ensure that the
$designobject actually has an associated project. You can do this by checking if$design->projectis not null before trying to access theassigneesrelationship. -
Make sure that the
projectrelationship is correctly defined in theDesignmodel. -
Check if the
assigneesrelationship is correctly defined in theProjectmodel.
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.