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,520 points
3,076 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,840 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,840 points
15,186 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,840 points
13,406 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,840 points
6,713 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,840 points
4,978 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,840 points
23,012 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,840 points
2,994 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,840 points
2,918 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,840 points
44,689 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,840 points
27,957 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,840 points
3,291 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP