【问题标题】:Laravel validator not works with custom request and added paramsLaravel 验证器不适用于自定义请求和添加的参数
【发布时间】:2023-01-13 12:18:11
【问题描述】:

我在 laravel 中有一个自定义请求类和一个服务类来处理我的应用程序中的所有请求。 Theese 将处理具有两个不同控制器的 api 和 web 请求。但是在 Web 控制器中,当我尝试动态添加参数以请求时,validated() 方法无法识别它们。

网络控制器:

    public function index(ShopIndexRequest $request, ShopService $service)
    {
        $request->mergeIfMissing(['items' => 3]);
        //other tries to adding parameters
        //$request->request->add(['items' => 3]);
        //request()->request->add(['items' => 3]);
        //$request['items'] = 3;
        $shops = $service->getall($request);
        
        return view('shop.index', compact('shops'));
    }

服务等级:

    namespace App\Services;
    
    use App\Http\Requests\Shop\ShopIndexRequest;
    use App\Http\Resources\ShopResource;
    use App\Models\Shop;
    
    class ShopService{
    
        public function getAll(ShopIndexRequest $request)
        {
            $validated = $request->validated();
            $query = Shop::query();
    
            if(isset($validated['name'])){
                $query->where('name', 'like', '%' . $validated['name'] . '%');
            }
    
            if(isset($validated['orderBy']) && isset($validated['orderDirection'])){
                $query->orderBy($validated['orderBy'], $validated['orderDirection']);
            }
    
            if(isset($validated['items'])){
                return ShopResource::collection($query->paginate($validated['items']));
            }
            else{
                return ShopResource::collection($query->paginate(config('app.defaultItemsPagination')));
            }
    
        }
    }

请求类:

    namespace App\Http\Requests\Shop;
    
    use Illuminate\Foundation\Http\FormRequest;
    
    class ShopIndexRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array<string, mixed>
         */
        public function rules()
        {
            return [
                'items' => 'integer|nullable',
                'name' => 'string|nullable',
                'orderBy' => 'string|nullable',
                'orderDirection' => 'string|nullable'
            ];
        }
    }

在控制器中调用 validated() 方法后,它总是给我一个空数组。然而,在 api 控制器中,它与邮递员一起工作得很好,接收我在每个请求中提供的参数,

这是 api 控制器中的索引方法:

    public function index(ShopIndexRequest $request, ShopService $service)
    {
        $shops = $service->getAll($request);
        return $shops;
    }

为什么 validated() 方法会忽略我使用所有这些方法动态添加的数据?这是实现我目标的一种方式吗?

【问题讨论】:

    标签: api service request laravel-9


    【解决方案1】:

    您可以模拟该请求。然后,在测试中使用 validated() 作为请求的模拟方法,并设置对 validated 的期望以返回您想要的值。

    【讨论】:

      猜你喜欢
      • 2017-05-30
      • 2015-07-05
      • 2020-12-10
      • 2021-04-13
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多