从单列数组Laravel获取ID

时间:2018-08-13 13:58:44

标签: php laravel laravel-5

我有这样的数据库

|id|      email     |departement_id|
|1 |test1@gmail.com |1,2           |
|2 |test2@gmail.com |3,4           |
|3 |test3@gmail.com |5,1           |
|4 |test4@gmail.com |2,6           |

我试图从id获取departement_id的数据以发送电子邮件。如果请求POST departement_id为2,则id必须为1和4。如何在Laravel中获得ID?因为单列departement_id中的数据是数组。

这是我的发送电子邮件功能...

Mail::send('mails.thanks',['ticket'=>$request, 'department' => $department], function ($message) use ($departement){
                $message->from(Config::get('const.email'), 'Ticket Plus');
                $message->subject('Nueva tarea creada');

                // get id user and send email user
                $getUsers = User::where('department_id',  [(int)$departement])->pluck('id')->toArray();
                $role_users = DB::table('role_user')
                             ->whereIn('user_id', $getUsers)
                             ->where('role_id', 2)
                             ->pluck('user_id')
                             ->toArray();
                $getMailUsers = DB::table('users')
                             ->whereIn('id', $role_users)
                             ->pluck('email')
                             ->toArray();

                foreach ($getMailUsers as $getMailUser)
                {
                    $message->to($getMailUser);
                };
                // $message->to($settings->admin_email);
            });

非常感谢..

1 个答案:

答案 0 :(得分:3)

Users = User::whereRaw('FIND_IN_SET('.(int)$departement.', department_id) > 0')