Undefined variable not laravel when trying to send email

0

I need to send email to more than one person, for this I've created a for replay structure, but I'm getting my variable undefined.

I tried something like:

public function avisarAnjos(Request $request)
{
        $usuariosAnjos = User::select('email')
               ->where('usuario_anjo', 1)
               ->get();


        $data = array(
            'lat' => $request->lat,
            'lng' => $request->lng,
            'foto' => $request->foto
        );

        for($i=0;$i < count($usuariosAnjos); $i++){
            Mail::send('email', $data, function ($message){
            $message->from('[email protected]', 'teste!');
            $message->to($usuariosAnjos[$i]);
        });
    }
    return response()->json("Email enviado com sucesso", 201);
}

If I put a return response()->json($usuariosAnjos, 201); I have a array with two emails, I do not understand why it says that this variable is undefined.

    
asked by anonymous 22.12.2018 / 18:54

1 answer

0

To use a function that is out of scope use "use" there in Mail :: send ();

public function avisarAnjos(Request $request)
    {
            $usuariosAnjos = User::select('email')
                   ->where('usuario_anjo', 1)
                   ->get();


            $data = array(
                'lat' => $request->lat,
                'lng' => $request->lng,
                'foto' => $request->foto
            );

            for($i=0;$i < count($usuariosAnjos); $i++){
                Mail::send('email', $data, function ($message) use $usuariosAnjos{
                $message->from('[email protected]', 'teste!');
                $message->to($usuariosAnjos[$i]);
            });
        }
        return response()->json("Email enviado com sucesso", 201);
    }
    
27.12.2018 / 21:50