Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ash92's avatar
Level 1

How to send email to a specific user with specific data from database tables?

I have three tables. One is users, where I have stored property_id, email and user_type. Another one is bills where i have stored bill information with property_id. Last one is property. Where I have stored all the properties. i am creating the bill against properties. i have many records in my bills and users tables. if I send email of bills it sends all the records from bills table to all the users from users table. But I want to send bill to specific user which match with property_id. property_id of users id and property_id bills table I need to bind and send email to that specific users with the specific bills information from bills table. How to do that? I got stuck here. Anyone please?

$users = User::where('user_type', 'residence_admin')->get();
    foreach($users as $user) {
        $data["email"] = $user->email;
        $data["title"] = "From Admin";
        $data["body"] = "Bill To Pay";
  
        $pdf = PDF::loadView('emails.myTestMail', $data);
  
        $mail = Mail::send('emails.myTestMail', $data, function($message) use($data, $pdf) {
            $message->to($data["email"], $data["email"])
                    ->subject($data["title"])
                    ->attachData($pdf->output(), "Bill.PDF");
        });
    }
0 likes
4 replies
Ash92's avatar
Level 1

@SilenceBringer I want to email a copy of bill to admins. But in my users table a lot of admin exist and in my bill table a lot of bill records exists. if I want to send a bill to an admin all the records from bills table is sending to all admins. But I want to send specific bill to specific user. Like admins property_id is 1 and bills property_id 1, then bill will be sent to that user only with the property_id 1 bill.

Snapey's avatar

@Ash92 take the laravel from scratch series here. what you want to do is easy if you have models and relations setup, but you don't mention either in your question

Please or to participate in this conversation.