Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajithlal's avatar

accessing laravel json response in vue didn't receiving the message

I'm using Vue on my client-side. On one of my controllers, I'm checking the condition and returning responses with error codes. The issue is I can't access the message in Vue that I'm passing from the controller. I'm getting the error code message.

Controller

public function store(BookingAddOnsFormRequest $request)
    {
        $booking = Booking::whereId($request->booking_id)
            ->whereUserId(auth('client')->user()->id)->first();

        if (!$booking->canBookAdditional()) {
		//here I'm passing the message with status code 400.
            return response()->json(['message' => trans('book_additional.booking_closed')], 400);
        }

        $addOnBooked = BookingAddOn::create($request->getData());

        return response()->json(['addon' => $addOnBooked, 'message' => trans('book_additional.add_on_added')], 201);
    }

Vue

add() {
            axios
                .post("/laiylaty/add-on", {
                   // some action
                })
                .then(response => {
                    //some action
                    this.notificationAlert("success", response.data.message);
                })
                .catch(response => {
                    //some action
			//here when I do console log I'm getting
			//Error: Request failed with status code 400 
			//instead of the actual message that I'm passing from the action
                    this.notificationAlert("error", response.message);
                });
        },
0 likes
2 replies
kkhicher1's avatar
Level 2

Your Code look fine. can you share your request from browser's dev tool network tab....

ajithlal's avatar

@kkhicher1 thanks for your response. The issue was with my server. Not on the code. It's working fine now.

Please or to participate in this conversation.