PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to group array by user_id in Laravel 8

 April 19, 2022     arrays, laravel, loops, php     No comments   

Issue

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.

+

this the database looks like:

enter image description here


Solution

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";
}


Answered By - N69S
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing