How to return a collection of objects in laravel via ajax?

1

I created this function in my Controller to return a list of employees in my ajax request, but it is not returning anything.

public function getFuncionarios(Request $request){
    if($info = Funcionario::where('id_empresa', $request->get('empresa'))){
         return response()->json($info);
    }else{
         return 'error';
    }
}

WhenIreturnthedirectvariableitalsodoesnotwork.

return$info;

ButifIputwheretoreturnonly1recordworks.

$info=Funcionario::where('id_empresa',$request->get('empresa'))->first()

What can it be? What would be the right way to get the result I need? This list of employees would be to feed a <select> .

    
asked by anonymous 01.08.2016 / 15:28

2 answers

1

To return a collection of objects, it can also be done as follows:

public function getFuncionarios(Request $request){

      $funcionarios = Funcionario::whereEmpresa($request->empresa)->get();
      if (!$funcionarios){
           return response()->json(['error' => 'Nenhum funcionário foi encontrado.']);
      }
      return response()->json($funcionarios);
}

to return a specific object:

public function getFuncionaio($id){

          $funcionario = Funcionario::find($id);

          return response()->json($funcionario);
    }
    
01.08.2016 / 18:59
2

I gave it soft ... I just put get at the end.

if($info = Funcionario::where('id_empresa', $request->get('empresa'))->get()){
    return $info;
}
    
01.08.2016 / 15:58