在这里,我使用Laravel 11,将vue3替换为Blade . 所以我在inertiaJs的帮助下在Laravel框架内使用vue3

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 im using laravel web route enter image description here

推荐答案

您是否try 返回,但您的laravel控制器出现错误?例如:

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

那么您的错误应该会在onMessage内部接收,如下所示:

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

Laravel相关问答推荐

如何解决此 Backblaze B2 S3 兼容 API 错误?

根据路径的开头重定向

RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException

即使上传了文件,Laravel Input::hasFile('image') 也会返回 false

在 laravel 5 中的表单请求验证后传递旧输入

查询 Laravel Select WhereIn 数组

我怎样才能了解更多关于我的 Laravel 排队作业(job)失败的原因?

查询构建器中的 Laravel 5 多重 Select ()

Laravel 5.4 LengthAwarePaginator

如何在 Laravel 中创建类别的嵌套列表?

如何从 Windows 命令提示符在 Laravel 中运行 PHPUnit

Laravel Socialite 在本地主机上进行测试,SSL 证书问题?

反向代理后面的 Laravel 路由

如何从 Laravel 中的资源中获取图像?

Laravel Queue,Beanstalkd vs Database,有什么区别?

在 Laravel 5.4 中将文件存储在公共目录和存储中的区别

控制器中的laravel foreach循环

在 Laravel 中结合 AND/OR Eloquent的查询

Laravel 部署……有标准的方式吗?

如何在 laravel 表单验证错误消息中给出自定义字段名称