0

I am making an e-commerce website in which I want to send emails to users with different roles once order was completed. I was able to do this for a specific role only but how do I do this for different roles? Since normal users can be sellers and sellers can also order products from other sellers. So far these are the steps I've tried:

OrderController.php

$users = User::whereHas('role', function ($q) {
        $q->where('name', 'user', 'seller');
    })->get();

    Mail::to($users)->send(new OrderCompleted($suborder));

However, this only works and sends email to the user roles and not the seller roles.

This is my mail model:

OrderCompleted.php

public $suborder;

public function __construct(SubOrder $suborder)
{
    $this->suborder = $suborder;
}

I have also tried Mail::to($suborder->user->email)->send(new OrderCompleted($suborder)); wherein my SubOrder.php model are as follows:

 public function user()
{
    return $this->belongsTo(User::class);
}

But when I try to use this second query, I get an error Trying to get property 'email' of non-object

How do I make this happen? Any advice would be much appreciated. Thank you!

Jemy
  • 129
  • 2
  • 12
  • `$users` is a collection , where `$suborder->user->email` is not a collection – STA Oct 24 '20 at 15:21
  • I think the first query I made was better since I'm not getting errors. However, when I use that the email only sends to 'user' roles. So if a seller made an order from another seller and order was marked as completed, email would be sent to a random 'user' role and not the seller role. – Jemy Oct 24 '20 at 15:22

2 Answers2

1

By your this line, "sends email to the user roles and not the seller roles.". I believe that you want to check name for both 'user' & 'seller'. So change your query,

$users = User::whereHas('role', function ($q) {
        $q->whereIn('name', ['user', 'seller']);
    })->get();

Your $q->where is only chacking for second argument i.e user in the case.

And for your second question I believe @sta is correct.

bhucho
  • 3,903
  • 3
  • 16
  • 34
  • Hi! Thank you for your response. I have tried this but it seems like only the 'user' role was recognized by the query. I tried making an order from a seller account and when I tried to mark it as completed from another seller's account, the email was sent to another 'user' email instead and not to the seller role's email who ordered it. – Jemy Oct 24 '20 at 15:26
  • Is there a way I can just directly send the email to all roles? – Jemy Oct 24 '20 at 15:29
  • 1
    what are you getting if you do `dd($users);`, also by directly do you mean you want to send mail by just entering the email of the user along with email body, also possible give some example how the data in your User model (table associated with the model) looks like – bhucho Oct 24 '20 at 16:31
  • If I do `dd($users)` I can get the array of users(wherein I have 3) and sellers(2) which is correct. Ex. The order was placed by user1. However when I try to mark the order as completed, email was sent to user3's email instead and not to user1's email. My table in my User model has columns `id, role_id(which is a foreign key for my roles table), name, email and password`. My roles table has columns `id, name and display_name`. I have 3 roles which are admin, user and seller. So my only problem now is the email being redirected to a wrong email address. – Jemy Oct 26 '20 at 14:01
  • 1
    is your dd($users) returns a collection of users having name & email field in each of them with data in the fields of email & name for each of them – bhucho Oct 26 '20 at 15:16
  • 1
    can you try looping over the recipients using the example shown in docs https://laravel.com/docs/8.x/mail#looping-over-recipients – bhucho Oct 26 '20 at 16:41
  • Yes, my `dd($users)` returns a collection of users in an array complete with all the details. I tried looping overt the recipients (user and seller roles) and it works fine and send email to every recipient. I was able to solve my problem using this `Mail::to($suborder->order->user->email)->send(new OrderCompleted($suborder));` since I made relationships through my models. Thank you so much for your help! :) – Jemy Oct 26 '20 at 18:45
  • ok, enjoy the feeling of solving your problem yourself, that is what makes coding fun :) – bhucho Oct 26 '20 at 18:50
1

I was able to send emails to target user emails regarless of their roles using

Mail::to($suborder->order->user->email)->send(new OrderCompleted($suborder));

Thank you so much!

Jemy
  • 129
  • 2
  • 12