Issue
I'm looking for advice for a project and an interesting predicament I've put myself in.
I have multiple fields in my shipment model, they are:
- shipto_id
- shipfrom_id
- billto_id
They all link (through different relationships) to my customer model:
public function billtoAccount()
{
return $this->belongsTo('App\Customer', 'bill_to');
}
public function shiptoAccount()
{
return $this->belongsTo('App\Customer', 'ship_to');
}
public function shipfromAccount()
{
return $this->belongsTo('App\Customer', 'ship_from');
}
and these customers (in reality would likely be better described as customer accounts (these are NOT USER ACCOUNTS, they're more just like a profile for each company that business is done with)) can have a multitude of users associated with them.
public function users()
{
return $this->belongsToMany(User::class);
}
Now, while I know how to send off mailables and notifications, I was curious to know how I would go about sending off those to multiple user's emails. So let me describe the following: Something is created and the following customers (and in turn, their users) are referenced.
- (billto_id) - Customer Account 1 - User 1 (email1@example.com)
- (shipto_id) - Customer Account 2 - User 2 (email2@example.com) & User 3 (email3@example.com)
- (shipfrom_id) - Customer Account 37 - User 6 (email4@example.com)
Now, how would I go about moving the emails of the users over to an array of emails to have a notification or mailable sent to them?
So it should pop out: email1@example.com, email2@example.com, email3@example.com, email4@examples.com
Solution
Elaborating on @Devon 's comment:
This is business logic. You could have a method on your Shipment model that returns the customer instances to be notified as an array, e.g. getNotifiables() : array
Then in your Customer model you may use the Notifiable trait
use Illuminate\Notifications\Notifiable;
And looping over your Notifiables, i.e. Customers
$notification = new ShipmentWasCreated()
foreach ($shipment->getNotifiables() as $notifiable) {
$notifiable->notify($notification);
}
Answered By - macghriogair
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.