0

I have this eloquent model query

public function getemployee(){
    $id = $_POST['id'];
    $employees = mot_users::where("branch_no", $id)->lists('user_no', 'lastname', 'firstname');
    return response()->json(['success' => true, 'employees' => $employees]);
}

the problem is this "->lists();" can only handle 2 columns which sent as a json response (seen through console, only 2 selected columns out of 3 is sent). How can I send 3 columns or more than 2 columns in the "->list()" or is there any alternative eloquent model query other than using "->list()" if its impossible?

Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164

1 Answers1

0

To do this, just do a

$employees = mot_users::where("branch_no", $id)->get(array('user_no','lastname','firstname'))->toArray();

That should do the trick.

Note that if your model object has a relationship and this column relationship isn't part of what you try to ->get you can't get an error.

Note 2 that here you're using the good ol' $_POST, you should consider using the proper Request object as seen in the doc.

Yoann Chambonnet
  • 1,313
  • 16
  • 22
  • thank you! @YoannCh, in regards of using proper request object, if i use "Request::input("id");" I get an error, however, if i use $_POST, i dont get one so I did use $_POST instead of "Request::input("id"); " even its inappropriate because im working with a framework. – Juliver Galleto May 12 '15 at 08:43
  • Then you may have a problem in your controller or in your design. You should resolve that problem too @Code_Demon. Feel free to accept my answer btw ;) – Yoann Chambonnet May 12 '15 at 23:43
  • I declared this in my controller "use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Routing\UrlGenerator;" anything im missing or something wrong why i get an error if i use proper request object? – Juliver Galleto May 13 '15 at 01:28
  • 1
    You should add the following : `use Illuminate\Http\Request;` – Yoann Chambonnet May 13 '15 at 02:42
  • 1
    and then in your method prototype, the one that does the @Post doing the following : `public function yourFunctionWhateverItsName(Request $request)` – Yoann Chambonnet May 13 '15 at 02:43
  • then you should be able in the method to do `$id = $request->input('id');` – Yoann Chambonnet May 13 '15 at 02:44
  • 1
    That's the dependency injection way. I prefered split all in separate messages so that it would look more clear I think and more readable. – Yoann Chambonnet May 13 '15 at 02:45