Why not a known variable?

This situation. Do verified users on the box. Why wrote that $user is undefined in the line Mail?

PS. Data come, and arrays are not empty.

public function signup(Request $request)
{
 $user = array(

 'firstname' => $request->firstname,
 'lastname' => $request->lastname,
 'middlename' => $request->middlename,
 'gender' => $request->gender,
 'email' => $request->email,
 'password' => $request->password,
 'password_confirm' => $request->password_confirm,
 'confirm_code' => encrypt(str_random(30)),

);

 $rules = array(
 'firstname' => 'required',
 'lastname' => 'required',
 'middlename' => 'required',
 'email' => 'required|email|unique:users',
 'password' => 'required',
 'password_confirm' => 'required|same:password'
);

 $validator = Validator::make($user,$rules);

if($validator->fails())
 return response() -> json(array(
 'fail' => true,
 'errors' => $validator->getMessageBag()->toArray()
));
 else {
 $user['password'] = bcrypt($user['password']);

 $data = array(
 'username' => $user['firstname'],
 'confirm_code' => $user['confirm_code'],
);

 if (User::create($user)) {
 Mail::send('email_verify', $data, function($message) {
 $message->to($user['email'], $user['firstname'])
 ->subject('registration Confirmation');
 }); 

 return response() -> json(array(
 'success' => 'You have successfully registered. Check your Inbox and activate your account.'
));
}
 else {
 return 'false';
}
}
 }
July 2nd 19 at 18:05
1 answer
July 2nd 19 at 18:07
Solution
Most likely what You are using there so called kolbak function (or in Russian - the function of the circuit), and the area of its visibility is usually strictly limited.

I think the situation might be rectified like this:
Mail::send('email_verify', $data, function($message) use ($user) { ...


Here are a few details.

Find more questions by tags Laravel