Reputation: 10720
Given several filters, I want to pass an array of emails and names from the User.php
model to an on-demand notification.
Unfortunately, inside the User.php model I got this appends:
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'profile_photo_url',
];
which cancels all the helpers Arr::except()
, Arr::only()
... all of them are useless to get rid of the profile_photo_url
. Ok, i got rid of it by commenting it out.
So, I try to get the email and name columns from the User like so:
ScheduleClass.php
$users_ec_ = User::where('area_id',$area_value_especial->id)->get(['email','name'])->toArray();
when I dd($users_ec_)
I get:
[
0 => array:1 [
"[email protected]" => "JUán perez"
]
When I try to call the notification:
Notification::route('mail',$users_ec_)
->notify(new Signed($eventos_ec));
;
Signed.php
<?php
namespace App\Notifications;
use App\Models\Meeting;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
class Firmadas extends Notification implements ShouldQueue
{
use Queueable;
private $meetings;
public function __construct(Collection $meetings)
{
$this->meetings = $meetings;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$meetings = $this->meetings;
return (new MailMessage)
->subject('New signatures...')
->markdown('emails.signed',compact('meetings'))
;
}
}
I get the following error, as for the users array:
[2022-09-28 09:23:44] local.ERROR: Attempt to read property "email" on array {"exception":"[object] (ErrorException(code: 0): Attempt to read property "email" on array at /home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php:228) [stacktrace]
What am I missing? How do I solve it?
According to the docs:
"provide an array that contains the email address as the key and the name as the value of the first element in the array"
ScheduleClass.php
$users_ec_ = User::where('area_id',$area_e_c->id)->get(['email','name'])->toArray();
//given that in User.php Model there's this "protected $appends" profile_photo_url, I had to set my own foreach to get rid of the `profile_photo_url` key, because that `protected $appends` cancels out all the Array Laravel helpers, such as Arr::except Arr::only ... etc
$users_ec=[];
foreach ($users_ec_ as $user){
$users_ec[$user['email']] = $user['name'];//This is key according to the docs
}
//To finally pass the email and name array to the Notification::route method:
Notification::route('mail',$users_ec)
->notify(new Signed($meetings_ec));
;
That did the trick
Upvotes: 0
Views: 1049
Reputation: 66
Your user array is a nested array.
The Laravel documentation https://laravel.com/docs/9.x/notifications#on-demand-notifications
Notification::route('mail', [
'[email protected]' => 'Barrett Blair',
])->notify(new InvoicePaid($invoice));
Try with $users_ec_[0] to see if it works. Then you can always work your way around the toArray() by building the array yourself or looping through the entries to send notifications
Upvotes: 1