get('project_id')) { $query->where(function ($q) use ($projectId, $equipmentRelation) { $q->where('project_id', $projectId); if ($equipmentRelation) { $q->orWhereHas("{$equipmentRelation}.productionLine", fn ($lq) => $lq->where('project_id', $projectId)); } }); } if ($lineId = $request->get('production_line_id')) { $query->where(function ($q) use ($lineId, $equipmentRelation) { $q->where('production_line_id', $lineId); if ($equipmentRelation) { $q->orWhereHas($equipmentRelation, fn ($eq) => $eq->where('production_line_id', $lineId)); } }); } } protected function syncScopeFromLine(array $data): array { if (! empty($data['production_line_id'])) { $line = ProductionLine::find($data['production_line_id']); if ($line?->project_id) { if (empty($data['project_id'])) { $data['project_id'] = $line->project_id; } elseif ((int) $data['project_id'] !== (int) $line->project_id) { throw new \InvalidArgumentException('خط تولید به پروژه انتخاب‌شده تعلق ندارد.'); } } } return $data; } protected function scopeValidationRules(): array { return [ 'project_id' => ['nullable', 'exists:pjt_projects,id'], 'production_line_id' => ['nullable', 'exists:maintenance_production_lines,id'], ]; } protected function equipmentIdsValidationRules(bool $required = true): array { return [ 'equipment_id' => array_filter([ $required ? 'required_without:equipment_ids' : 'nullable', 'exists:maintenance_equipment,id', ]), 'equipment_ids' => ['nullable', 'array', 'min:1'], 'equipment_ids.*' => ['integer', 'exists:maintenance_equipment,id'], ]; } protected function resolveEquipmentIds(array $data): array { if (! empty($data['equipment_ids']) && is_array($data['equipment_ids'])) { return array_values(array_unique(array_map('intval', $data['equipment_ids']))); } if (! empty($data['equipment_id'])) { return [(int) $data['equipment_id']]; } return []; } }