I believe that if you put your logic in a controller you would be better off instead of trying to put the logic in a route. I know for json to work I had to put this use statement at top of controller:
use Illuminate\Contracts\Routing\ResponseFactory;
and method looks like:
public function jsontest(ResponseFactory $response) {
$dummy = $_GET['id'];
$mydata = \Illuminate\Support\Facades\DB::table('powners')
->where('oname', 'like', 'D%')
->orderBy('oname', 'asc')->get();
return $response->json($mydata);
}
Notice the ResponseFactory $response part.
Sorry I do not know how or would I ever cram code into a route.
I try to use clean routes like:
Route::get('tryjson', array('uses' => 'PownersController@jsontest'));
my json post request has a data key named "id", now how im going to pass it from routes to my controller?
my json post request
$.post("/employees", {id:"1"}, function(response){
if(response.success)
{
var branchName = $('#branchname').empty();
$.each(response.employees, function(){
$('<option/>', {
value:$(this).user_no,
text:$(this).firstname
}).appendTo(branchName);
});
}
}, 'json');
as you can see from my json post request i have put a key value name id
my routes
Route::post('employees', [
'as' => 'employees', 'uses' => 'mot@getemployee'
]);
and my controller
public function getemployee($id){
$employees = employees::where("branch_no", $id)->lists('firstname', 'user_no');
return response()->json(['success' => true, 'employees' => $employees]);
}
as you can see, I have an argument $id, it supposedly, its where the key value named id from the json post request will be put and be used from the query stuff.
Assuming you use Laravel 5
Route::post('employees/{id}', [
'as' => 'employees', 'uses' => 'mot@getemployee'
]);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community