我有一个包含组织产品的表。该系统将以多租户的方式包含多个组织。
我的products
表有以下列:
id organisation_id product_id other_columns
我已经为products
创建了一个请求,现在我想验证来自create product
表单的输入。要求product_id
对于这个organisation_id
是唯一的
我的request
中的rules()
方法是
public function rules()
{
return [
'part_number' => 'required|max:20|unique:products,part_number,'.$request->organisation_id.',organisation_id',
'other_fields_here' => ...
];
}
以上内容不起作用,因此我未能理解Laravel文档。谢谢你的指点。
因此您希望元组(organization_id,part_number)是唯一的。试试看:
// ... other rules
'part_number' => 'unique:products,part_number,NULL,id,organisation_id,'.$organisation_id,
// other rules ...
选项为:unique:table,field,id_to_be_ignored,id_field_of_the_table,additional_where_conditions
其中additional where条件是要进行比较的对,因此在您的案例中,字段organization_id和值$organization_id相等。
答案是:
public function rules()
{
return [
'part_number' => 'unique:products,part_number,NULL,id,organisation_id,' . $orgId),