php - 具有两个字段的数组上的 Laravel 验证存在于数据库行上
问题描述
我有一个输入数组,每个项目包含 2 个字段:
"items" :[
{
"product_id" : 23232,
"product_offer_id" : 3434
},
{
"product_id" : 4545,
"product_offer_id" : 67676
}
]
我想验证product_offer_id
and是否product_id
存在于products
表中。当前规则:
$rules = [
'items' => 'required',
'items.*.product_id' => 'required|exists:products,id',
'items.*.product_offer_id' => 'required|exists:product_offers,id',
'items.*.quantity' => 'required|numeric',
];
没有数组我这样做:
$product_id = \Input::get('product_id');
$rules = [
'product_id' => 'required|exists:products,id',
'product_offer_id' => [
'required',
\Illuminate\Validation\Rule::exists('product_offers','id')
->where(function ($query) use ($product_id) {
$query->where('product_id', $product_id);
})
]
];
解决方案
您将需要根据您使用 Laravel 类验证器编写的规则运行包含您的项目的输入数组。
Validator::make($yourInputArray, $rules)->validate();
推荐阅读
- reactjs - 在“Connect(DiveLogTable)”的上下文中找不到“store”。要么将根组件包装在
Redux 组件中的错误 - python - 计划模块计时问题
- python - 如何格式化具有不同数组大小的 Pandas DataFrame
- c - 在 SDL 中绘制大量矩形到屏幕的正确和最有效的方法
- django - ValueError:格式错误的十六进制 UUID 字符串
- ruby-on-rails - 无法推送到heroku,sassc分段错误
- linux - 在 nspawn 容器中运行时,具有私有网络的 Systemd 服务失败
- google-chrome-extension - 有没有办法更改 chrome 扩展弹出窗口的位置/位置?
- javascript - 反应 CSS 样式
- delphi - TRelativePanel 循环依赖