Manjunarth
Manjunarth

Reputation: 323

how to do server side api validation in laravel 5.4

How to send send error message in json format to postman in laravel 5.4

 //Controller
 public function store(Request $request)
 {
     $validator = Validator::make($request->all(),[
        'department_name'  => 'required',
     ]);
     if($validator->fails())
     {
        return $validator->errors()->all();
     }
     Department::create($request->all());

     return Response::json(['message' => 'Added','status' => 201],201);
}

Upvotes: 2

Views: 1352

Answers (1)

Maraboc
Maraboc

Reputation: 11083

You can simply return the validation errors as json response like this :

if ($validator->fails()) { 
    return response()->json(['errors'=>$validator->errors()]); 
}

Upvotes: 2

Related Questions