使用laravel将电子邮件传递给父用户

User_id     parent_id        email          username
1                abc@gmail.com      abc
2                xyz@gmail.com      xyz
3       5        data@gmail.com     
4                nod@gmail.com      nodata
5                getPar@gmail.com   parent

I want to send email with providing username but where parent_id exist it doesnot have username in that case we will pick parent_id = user_id it mean user_id =3 having username parent.

So how can achive this with ease.

I have done so far

$user = User::where('email', Input::get('email'))
        ->where('type', Input::get('type'))
        ->active()
        ->first();
$data = [
            'name'     => $user->name,
            'email'    => $user->email,
            'username' => $user->username
        ];


 if($user->username == "" && $user->parent_id){
            $userParent = User::where('user_id', $user->parent_id)
                        ->active()
                        ->first();
        }

Mail::later(5, 'emails.send', $data, function($message) use ($user)
        {
            $message->from('n-r@abc.com', 'abc');
            $message->to($user->email, $user->name)->subject('ABC - Recovered Username');
        });

return Response::json([
            'success' => true,
            'message' => 'Recovered Username',
            'data'    => [
                'username' => $user->username
            ]
        ], 200);

On such requirement want to fetch that data where username is == '' && parent_id avail than send the email to that user who has parent_id = user_id in this above example parent_id = 5 than user_id will be = 5 so if someone invoke this than it should email to getPar@gmail.com along with its username

Just understand the concern

User_id     parent_id        email                      username
1                     abc@gmail.com             abc
2                         abc@gmail.com             xyz
3        5                abc@gmail.com     
4                         abc@gmail.com             nodata
5                         data@gmail.com            parent

all have same email id except user_id 3 because it has parent_id so no username for this id

for this case we want to fetch parent_id's email id that is in user_id = 5 which is data@gmail.com we would sent username to this email id else who has username normal flow will continue

Change the line postion of $data, Replace if condition with while so that whenever username == '' and having parent id it will go for its parent. Also use $user instead of $userParent and pass $data.

$user = User::where('email', Input::get('email'))
        ->where('type', Input::get('type'))
        ->active()
        ->first();

while ($user->username == "" && $user->parent_id) {
    $user = User::where('user_id', $user->parent_id)
            ->active()
            ->first();
}

$data = [
    'name' => $user->name,
    'email' => $user->email,
    'username' => $user->username
];

Mail::later(5, 'emails.send', $data, function($message) use ($user) {
    $message->from('n-r@abc.com', 'abc');
    $message->to($user->email, $user->name)->subject('ABC - Recovered Username');
});

return Response::json([
            'success' => true,
            'message' => 'Recovered Username',
            'data' => [
                'username' => $user->username
            ]
                ], 200);