【问题标题】:validation getting failed in Laravel APILaravel API 中的验证失败
【发布时间】:2021-09-04 05:50:06
【问题描述】:

laravel 控制器验证失败,请帮忙

存储库:https://github.com/dhawlesudhir/basic_app.git

ProductController.php

  protected function validateRequest()
        {
            return request()->validate([
                'name' => 'required|min:10|max:255',
                'price' => 'required|integer|min:100',
                'category_id' => 'required|exists:categories,id'
            ]);
        }
    
        public function store()
        {
        
            $data = $this->validateRequest();
        
            $product = Product::create($data); 
    
            return new ProductResource($product);
        }

api.php:

Route::apiResource('/products', ProductController::class);

【问题讨论】:

  • 您是否在 headers 中将 Content-type 设置为 application/json ?在邮递员中

标签: php laravel api


【解决方案1】:

Laravel 抛出验证,因为你没有在邮递员中设置 json。 我可以看到目前你已经设置了Text

将类型设置为 json,如屏幕截图所示

否则 Laravel 从request()->all() 接收空数组

还要确保设置标题Accept:application/json

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 2021-10-06
    • 2014-08-16
    • 1970-01-01
    • 2020-10-28
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多