How to post the values into validation and return the response as json rather than return view in laravel blade

0 votes

I am trying to post the values into validation and return the response as json rather than return view as given in the documentation.

    $validator = Validator::make($request->all(), [
        'about' => 'min:1'
    ]);

    if ($validator->fails()) {
        return response()->json(['errors' => ?, 'status' => 400], 200);
    } 

The post is made by ajax so I need to receive the response in the ajax as well.

In order to prevent refresh of the page in the returning response, I have to give it a status code of 200 outside the array. But I couldn't figure out what to give the 'errors' part. What should I write in there?

Nov 12, 2020 in Laravel by kartik
• 37,490 points
507 views

1 answer to this question.

0 votes

Hii,

You can also tell Laravel you want a JSON response. Add this header to your request:

'Accept: application/json'

And Laravel will return a JSON response.

Hope it helps!!

answered Nov 12, 2020 by Niroj
• 82,680 points

Related Questions In Laravel

0 votes
1 answer

How to include a sub-view in Blade templates in Laravel?

Hello @kartik, You can use the blade template ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,680 points
5,520 views
0 votes
1 answer

How to break a for each loop in laravel blade view?

Hello @kartik, By default, blade doesn't have @break and @continue which are ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,680 points
5,604 views
0 votes
1 answer

How to get the id when you're validating in the model Validation unique on update using laravel?

Hello @kartik, in Laravel's inbuilt auth system, the ...READ MORE

answered Sep 11, 2020 in Laravel by Niroj
• 82,680 points
1,724 views
0 votes
1 answer

How to set .env values in laravel programmatically on the fly?

Hello @kartik, Since Laravel uses config files to ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,680 points
906 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,680 points
6,758 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,680 points
784 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,680 points
586 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,680 points
7,166 views
+1 vote
1 answer

How to load blade or php content into a view via ajax/jquery in laravel?

Hello @kartik, Assuming you're using jQuery... create a route ...READ MORE

answered Apr 14, 2020 in Laravel by Niroj
• 82,680 points
10,424 views
0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,680 points
263 views