MrRobot21
19901
16
Laravel

How to return validation errors as JSON to view

Posted 2 years ago by MrRobot21

Hi awesome people

i'm building an API, i'm validating the input fields in my StoreLesson.php

    public function rules()
    {
        return [
            'title' => 'required',
            'body' => 'required',
        ];
    }

i'm using postman to test the API, things are working fine but when i send POST request with empty fields in postman console in webview i'm getting redirected to welcom.blade.php

//LessonController.php
    public function store(StoreLessons $request) 
    {
        Lesson::create($request->all());

        return response()->json($validator->errors(), 422);  //i'm not getting any json with errors

        //Lesson::create(input::all());

        return $this->respondCreated('Lesson created successfully');
    }

i want to display the validator error as json

thank You

Please sign in or create an account to participate in this conversation.