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

  Kiến thức lập trình

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

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
enter image description here

LEAVE A COMMENT