Notification error , Undefined array key

Hello! I am making a notification but my domain_check_id is undefined array key, Anyone has a idea how to fix this i don't understand what is miss with it.
public function __construct(public User $user, public DomainCheck $domainCheck, public string $message, public string $categories)
{
}

public function via($notifiable): array
{
return ['database'];
}

public static function toFilamentNotification(): FilamentNotification
{
return FilamentNotification::make()
->constructUsing(function (array $data) {
[
'user_id' => $userId,
'domain_check_id' => $domainCheckId,
'message' => $message,
'categories' => $categories
] = $data;

return new static(
user: User::find($userId),
domainCheck: DomainCheck::find($domainCheckId),
message: $message,
categories: $categories,
);
})->message(fn(self $notification) => $notification->message)
->description(fn(self $notification) => $notification->categories);
}

public function toArray($notifiable): array
{
$terms = is_array($this->domainCheck->terms) ? implode(', ', $this->domainCheck->terms) : $this->domainCheck->terms;
$categories = is_array($this->domainCheck->categories) ? implode(', ', $this->domainCheck->categories) : $this->domainCheck->categories;

return [
'user_id' => $this->user->id,
'domain_check_id' => $this->domainCheck->id,
'message' => 'De domein checks zijn klaar voor: ' . $terms,
'categories' => 'De volgende categorienen zijn uit gekozen: ' . $categories,
];
}
public function __construct(public User $user, public DomainCheck $domainCheck, public string $message, public string $categories)
{
}

public function via($notifiable): array
{
return ['database'];
}

public static function toFilamentNotification(): FilamentNotification
{
return FilamentNotification::make()
->constructUsing(function (array $data) {
[
'user_id' => $userId,
'domain_check_id' => $domainCheckId,
'message' => $message,
'categories' => $categories
] = $data;

return new static(
user: User::find($userId),
domainCheck: DomainCheck::find($domainCheckId),
message: $message,
categories: $categories,
);
})->message(fn(self $notification) => $notification->message)
->description(fn(self $notification) => $notification->categories);
}

public function toArray($notifiable): array
{
$terms = is_array($this->domainCheck->terms) ? implode(', ', $this->domainCheck->terms) : $this->domainCheck->terms;
$categories = is_array($this->domainCheck->categories) ? implode(', ', $this->domainCheck->categories) : $this->domainCheck->categories;

return [
'user_id' => $this->user->id,
'domain_check_id' => $this->domainCheck->id,
'message' => 'De domein checks zijn klaar voor: ' . $terms,
'categories' => 'De volgende categorienen zijn uit gekozen: ' . $categories,
];
}
Solution:
i fixed it 😄 it was a stupid fault of me . I forgot to delete some old records i thought i did . Now it does the job just needed to edit the code for the id , It look like this now 😄 ``` public function __construct(public User $user, public DomainCheck $domainCheck, public string $message, public string $categories) {...
Jump to solution
17 Replies
Quin.
Quin.OP2y ago
No description
Dennis Koch
Dennis Koch2y ago
Check the toFilamentNotification() method. You are using some undefined variables. And probably want to use $this->...
Quin.
Quin.OP2y ago
i thought everything was right about this code. I couldn't find the missing piece. I am doing the same for the user as you can see in the code and that is working fine
Dennis Koch
Dennis Koch2y ago
Oh didn't see that you are destructuring. Is domain_check_id a Field with a relationship? That isn't included in $data then.
Quin.
Quin.OP2y ago
Nope it is just a field in
$data
$data
, is that really the problem? 😄
Dennis Koch
Dennis Koch2y ago
What's the dd($data)?!
Quin.
Quin.OP2y ago
array:6 [▼ // app/Notifications/CheckDomainNotification.php:36
"user_id" => 1
"message" => "De domein checks zijn klaar voor: dw"
"categories" => "De volgende categorienen zijn uit gekozen: "
"title" => ""
"body" => null
"format" => "filament"
]
array:6 [▼ // app/Notifications/CheckDomainNotification.php:36
"user_id" => 1
"message" => "De domein checks zijn klaar voor: dw"
"categories" => "De volgende categorienen zijn uit gekozen: "
"title" => ""
"body" => null
"format" => "filament"
]
Dennis Koch
Dennis Koch2y ago
So yeah. It's missing your expected domain_check_id. Where is that $data coming from? I don't know how that part works.
Quin.
Quin.OP2y ago
Filament
Notifications Pro by Ralph J. Smit - Filament
Seamlessly integrate Laravel database notifications with Filament.
Quin.
Quin.OP2y ago
How do you mean where the $data come's from that just a array ? Maybe i don't understand you correctly for that matter 🙂
Dennis Koch
Dennis Koch2y ago
Yes, but what is included into $data? Where does that content come from. I just see that array. I don't know the plugin you are using.
Quin.
Quin.OP2y ago
Filament
Notifications Pro by Ralph J. Smit - Filament
Seamlessly integrate Laravel database notifications with Filament.
Quin.
Quin.OP2y ago
or in the class self? like the FilamentNotification.php
Dennis Koch
Dennis Koch2y ago
I mean ->constructUsing(function (array $data) {. It doesn't include your expected data. But I don't even know where this data is coming from. How do you know what content it has?
Quin.
Quin.OP2y ago
You mean this part of that class right/
public function getConstructUsing($notificationClass): ?Closure
{
return $this->constructUsing ?? function (array $data) use ($notificationClass) {
$reflection = new ReflectionClass($notificationClass);

$constructorParameters = collect($reflection->getConstructor()->getParameters())
->mapWithKeys(function (ReflectionParameter $parameter) use ($data) {
if ( ! array_key_exists($parameter->getName(), $data) ) {
return [];
}

$value = $data[$parameter->getName()];

if ( $parameter->hasType() && is_subclass_of($type = $parameter->getType()->getName(), Model::class) ) {
$value = $type::find($value);
}

return [$parameter->getName() => $value];
})
->all();

$properties = collect($reflection->getProperties())
->mapWithKeys(function (ReflectionProperty $property) use ($constructorParameters, $data, $notificationClass) {
if ( ! array_key_exists($property->getName(), $data) ) {
return [];
}

if ( $property->class !== $notificationClass ) {
return [];
}

if ( $property->isPromoted() ) {
return [];
}

$value = $data[$property->getName()] ?? null;

return [$property->getName() => $value];
})
->all();

$notification = new $notificationClass(...$constructorParameters);

foreach ($properties as $property => $value) {
invade($notification)->{$property} = $value;
}

return $notification;
};
}
public function getConstructUsing($notificationClass): ?Closure
{
return $this->constructUsing ?? function (array $data) use ($notificationClass) {
$reflection = new ReflectionClass($notificationClass);

$constructorParameters = collect($reflection->getConstructor()->getParameters())
->mapWithKeys(function (ReflectionParameter $parameter) use ($data) {
if ( ! array_key_exists($parameter->getName(), $data) ) {
return [];
}

$value = $data[$parameter->getName()];

if ( $parameter->hasType() && is_subclass_of($type = $parameter->getType()->getName(), Model::class) ) {
$value = $type::find($value);
}

return [$parameter->getName() => $value];
})
->all();

$properties = collect($reflection->getProperties())
->mapWithKeys(function (ReflectionProperty $property) use ($constructorParameters, $data, $notificationClass) {
if ( ! array_key_exists($property->getName(), $data) ) {
return [];
}

if ( $property->class !== $notificationClass ) {
return [];
}

if ( $property->isPromoted() ) {
return [];
}

$value = $data[$property->getName()] ?? null;

return [$property->getName() => $value];
})
->all();

$notification = new $notificationClass(...$constructorParameters);

foreach ($properties as $property => $value) {
invade($notification)->{$property} = $value;
}

return $notification;
};
}
Dennis Koch
Dennis Koch2y ago
Well, that's the method you are overwriting. Still doesn't show the data that is injected via $data
Solution
Quin.
Quin.2y ago
i fixed it 😄 it was a stupid fault of me . I forgot to delete some old records i thought i did . Now it does the job just needed to edit the code for the id , It look like this now 😄
public function __construct(public User $user, public DomainCheck $domainCheck, public string $message, public string $categories)
{
}

public function via($notifiable): array
{
return ['database'];
}

public static function toFilamentNotification(): FilamentNotification
{
return FilamentNotification::make()
->constructUsing(function (array $data) {
[
'user_id' => $userId,
'domain_check_id' => $domainCheckId,
'message' => $message,
'categories' => $categories
] = $data;
return new static(
user: User::find($userId),
domainCheck: DomainCheck::find($domainCheckId),
message: $message,
categories: $categories,
);
})->message(fn(self $notification) => $notification->message)
->description(fn(self $notification) => $notification->categories);
}

public function toArray($notifiable): array
{
$terms = is_array($this->domainCheck->terms) ? implode(', ', $this->domainCheck->terms) : $this->domainCheck->terms;
$categories = is_array($this->domainCheck->categories) ? implode(', ', $this->domainCheck->categories) : $this->domainCheck->categories;
return [
'user_id' => $this->user->id,
'domain_check_id' => $this->domainCheck->id,
'message' => 'De domein checks zijn klaar voor: ' . $terms,
'categories' => 'De volgende categorienen zijn uit gekozen: ' . $categories,
];
}
public function __construct(public User $user, public DomainCheck $domainCheck, public string $message, public string $categories)
{
}

public function via($notifiable): array
{
return ['database'];
}

public static function toFilamentNotification(): FilamentNotification
{
return FilamentNotification::make()
->constructUsing(function (array $data) {
[
'user_id' => $userId,
'domain_check_id' => $domainCheckId,
'message' => $message,
'categories' => $categories
] = $data;
return new static(
user: User::find($userId),
domainCheck: DomainCheck::find($domainCheckId),
message: $message,
categories: $categories,
);
})->message(fn(self $notification) => $notification->message)
->description(fn(self $notification) => $notification->categories);
}

public function toArray($notifiable): array
{
$terms = is_array($this->domainCheck->terms) ? implode(', ', $this->domainCheck->terms) : $this->domainCheck->terms;
$categories = is_array($this->domainCheck->categories) ? implode(', ', $this->domainCheck->categories) : $this->domainCheck->categories;
return [
'user_id' => $this->user->id,
'domain_check_id' => $this->domainCheck->id,
'message' => 'De domein checks zijn klaar voor: ' . $terms,
'categories' => 'De volgende categorienen zijn uit gekozen: ' . $categories,
];
}

Did you find this page helpful?