My application creates a new entry through a POST
request at the api endpoint.
Now if any check fails, instead of returning a json error, laravel 5.3 redirects the request to the home page.
Here is my controller:
public function create( Request $request ) { $organization = new Organization; // Validate user input $this->validate($request, [ 'organizationName' => 'required', 'organizationType' => 'required', 'companyStreet' => 'required' ]); // Add data $organization->organizationName = $request->input('organizationName'); $organization->organizationType = $request->input('organizationType'); $organization->companyStreet = $request->input('companyStreet'); $organization->save(); return response()->json($organization); }
If there is no problem with validation, then the object will be successfully added to the database, but if there is a problem with validating the request, instead of sending all error messages in the form of a json response, it redirects back to the main page.
How can I set the validate return type to json, so with every request, if the validation fails, laravel will by default send all error messages as json.
source share