|string> */ public function rules(): array { return [ 'name' => 'required|max:100', 'code' => [ 'required', 'max:45', // Rule::unique('assets')->where(function ($query) { // $query->where('company_id', Auth::user()->company_id) // ->whereNull('deleted_at');// 确保忽略已删除的记录 // })->ignore($this->route()->parameter('asset')), ], 'status' => [ 'required', new Enum(AssetStatus::class), ], // 'owner' => [ // 'required' // Rule::exists('users', 'id') // ->where($this->userCompanyWhere()), // ], 'address' => 'max:255', 'group_id' => [ 'nullable', 'sometimes', Rule::exists('asset_groups', 'id') ->where($this->userCompanyWhere()), ], 'geo_address_code' => 'max:255', 'acl' => 'required', 'whitelist' => [ 'array', function ($attribute, $value, $fail) { $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count(); if ($userCount != count($value)) { $fail('The selected user is invalid.'); } } ], 'latitude' => 'numeric', 'longitude' => 'numeric', 'parent_id' => [ $this->parentIdExistsRule(), ] ]; } protected function parentIdExistsRule() { // 如果 parent_id 不为 0,返回 exists 规则 if ($this->input('parent_id') != 0) { return Rule::exists('assets', 'id')->where($this->userCompanyWhere()); } // 如果 parent_id 为 0,返回空数组以跳过 exists 验证 return []; } public function attributes() { return [ 'name' => __("fields.name"), 'code' => __("fields.code"), 'acl' => __("fields.acl"), 'address' => __("fields.address"), 'owner' => __("fields.owner"), 'status' => __("fields.status"), ]; } }