Return array of properties in laravel

1

I created a function in the controller that returns me the users where the user_anjo condition is equal to 1.

You are currently returning all of this user's data in json.

How can I do to return only the user's emails?

I tried something like:

public function avisarAnjos(Request $request){
    $usuariosAnjos = User::where('usuario_anjo', '1')->get();
    return response()->json($usuariosAnjos.email);
}

e:

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

and

public function avisarAnjos(Request $request){
    $usuariosAnjos = User::where('usuario_anjo', '1')->get();
    return response()->json($usuariosAnjos['email']);
}
    
asked by anonymous 22.12.2018 / 15:04

2 answers

2

To return only one field, use the select method (or until selectRaw ') and only bring the field (s) on the return builder , example :

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

This example will generate a : SELECT email FROM users WHERE usuario_anjo = ? , and the result only the filter mailing list.

22.12.2018 / 15:11
0
public function avisarAnjos(Request $request)
{
    $usuariosAnjos = User::where(['usuario_anjo'=>'1'])->get(['email']);
    return response()->json($usuariosAnjos);
}
    
26.12.2018 / 18:28