Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get back error form laravel to inertiajs/vue3 application?

Here I’m using Laravel 11 with vue3 replaced for blade.
so i’m using vue3 inside of Laravel framework with the help of inertiaJs

controller

 public function addInfo(Request $request){
        $validatedData = $request->validate([
            'title' => 'required|string|max:25',
            'message' => 'required|string',
            'days' => 'required|integer|min:1',
        ]);
        
        $count= Information::all()->count();
        if($count >= 5){
          
            $error = ([
             'message'=> 'intonation not allowed more then 5 please delete old one',
             'status' => false,
            ]);


            return response()->json($error); 
        }

        $new_info = new Information();
        $new_info->title    = $request->title;
        $new_info->content  = $request->message;
        $new_info->days     = $request->days;
        
        $new_info->links    = json_encode($request->links);
        $new_info->save();
    }

VUE3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const form = useForm({
   title: '',
   message: '',
   days: '',
   links:[],

});

 form.post(route('info.add'), {
        preserveScroll: true,
        onSuccess: () =>{ 
        
          form.reset();
            lastInput.forEach(lastInput => {
              lastInput.remove();
            });
            console.log(error);
          showAlert('success', 'Info added Successfully');
          
        },
        onError: () => {
        
    }
      });

i want to know that how can i get return response()->json($error); to show in vue onError: () or any other process to show the error im using laravel web route
enter image description here

>Solution :

Have you tried to return with error on your laravel controller ? for example:

return redirect()->back()->withErrors($error)

Then your error should be received inside onError, like this:

onError: (errors) => {
   alert(errors.message)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading