Home > database >  How to group array by user_id in Laravel 8
How to group array by user_id in Laravel 8

Time:04-01

I create the CRUD App. In this App, the user can add a contact, and include their birthday in that contact.

I have two tables in PHP MySQL: students and users. When their contact at the students' table has a birthday, the user will receive an email.

To test the email work, I'm using MailHog. It's was work.

I test that there are two users with a birthday in that contact. One user has a notification email. Another one has two notification emails because that contact has two data that are two birthdays.

The problem is, that I want to send one notification to every user. So if one user has many contacts on that birthday, it will give the user one email notification, instead of many emails.

this is the notification in MailHog:

enter image description here

user john has two notifications, instead of one.

this is my code:

 public function handle()
{
    
    info('mail first');
    sleep(1);
    info('mail second');
    $student = Student::whereMonth('birth', date('m-d'))
                        ->whereDay('birth', date('d') )
                        
                        ->get();

    foreach ($student as $mystudent) {

    $user = User::find($mystudent->user_id);


    Mail::to($user)->send(new WelcomeMail($mystudent));

    }

    // dd($userid);


    echo "check email";

   
} 

this code in class WelcomeMail

public function __construct($students)
{
    $this->data = $students;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{

    

    return $this->view('emails.welcome')->with('students', $this->data);
}

this is in view:

    {{ $students}}
    

I do this modiv in mail command:

 $userid = [];                  
    foreach ($student as $mystudent) {

    $user = User::find($mystudent->user_id);
    array_push($userid, $user);


    Mail::to($user)->send(new WelcomeMail($mystudent));

    }

At this point, it's still given me the result that the user has many emails for each contact.

hopefully, you help me train logic to solve this one.

CodePudding user response:

$student = Student::whereMonth('birth', date('m-d'))
                    ->whereDay('birth', date('d') )
                    ->GROUPBY('user_id')
                    ->get();

CodePudding user response:

You can start your query from the user, so you're sure that every user is present only once.

I'm assuming you have students() relation present on the User::class model

public function handle()
{
    $users = User::wherehas('students', function($studentQuery) {
        $studentQuery
            ->whereMonth('birth', date('m-d'))
            ->whereDay('birth', date('d'));
    })
        ->with('students', function($studentQuery) {
            $studentQuery
                ->whereMonth('birth', date('m-d'))
                ->whereDay('birth', date('d'));
        })
        ->get();

    foreach ($users as $user) {
        Mail::to($user)->send(new WelcomeMail($user->students));
    }
    echo "check email";
}
  • Related