vendredi 11 septembre 2020

Laravel API Validation errors to be displayed

I am valdating the fields sent through api and need to display the errors. I tried using try and catch no errors thrown. I have already have a code validating the login

        try {
            $request->validate([
                'email' => 'required|string|email',
                'password' => 'required|string',
                'remember_me' => 'boolean',
            ]);
        } catch (Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }

I found no errors return has json instead it is redirecting to the login page

How to handle rerros in API and sent the message as json?None of the example show the way to handle errors. I tried with everything

And also how to handle errors while creating the model

       try {
            $company = Company::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'country_code' => $data['country_code']]);
        } catch (Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire