简体   繁体   English

如何在 Laravel 中按 user_id 对数组进行分组 8

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

I create the CRUD App.我创建了 CRUD 应用程序。 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.我在 PHP MySQL 有两个表:学生和用户。 When their contact at the students' table has a birthday, the user will receive an email.当他们在学生桌的联系人生日时,用户将收到一个 email。

To test the email work, I'm using MailHog.为了测试 email 的工作,我使用了 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.一个用户有一个通知 email。另一个用户有两封通知电子邮件,因为该联系人有两个数据是两个生日。

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.因此,如果一个用户在那个生日那天有很多联系人,它将给用户一个 email 通知,而不是许多电子邮件。

this is the notification in MailHog:这是 MailHog 中的通知:

在此处输入图像描述

user john has two notifications, instead of one.用户 john 有两个通知,而不是一个。

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此代码在 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:我在邮件命令中执行此 modiv:

 $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:这个数据库看起来像:

在此处输入图像描述

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我假设您在User::class model 上存在students()关系

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";
}
$student = Student::whereMonth('birth', date('m-d'))
    ->whereDay('birth', date('d'))
    ->groupBy('user_id')
    ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM