G
G
gitdev2017-08-29 18:17:11
Laravel
gitdev, 2017-08-29 18:17:11

How to get errors in laravel during ajax request and validation and return them in json format?

I create web. project, there will be a browser version, and api for mob. devices. I take out an array that will be rendered into a separate method, I plan to use this array for the web. version and for api. You need to get errors from CreateCarRequest and pass them on to the application.
How to get an array of errors?

protected function renderArray()
    {
        $cars = Cars::where(['user_id' => auth()->user()->id])
            ->with('status')
            ->get()
            ->toArray();

        $renderArr = [
            'cars' => $cars,
            //'carStatuses' => CarStatus::all()->toArray()
        ];
        return $renderArr;
    }

    public function index()
    {
        return view('car.index', $this->renderArray());
    }
    public function storage(CreateCarRequest $request)
    { }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Dergachov, 2017-08-29
@vanillathunder

Add a validator. For example

$validator = Validator::make(
    array('name' => 'Дейл'),
    array('name' => array('required', 'min:5'))
);

And display errors
$validator->errors();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question