CreateOrUpdateRequest.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: kelyliang
  5. * Date: 2024/2/29
  6. * Time: 上午 11:37
  7. */
  8. namespace App\Http\Requests\API\Department;
  9. use App\Http\Requests\RuleHelper;
  10. use Illuminate\Foundation\Http\FormRequest;
  11. use Illuminate\Validation\Rule;
  12. class CreateOrUpdateRequest extends FormRequest
  13. {
  14. use RuleHelper;
  15. /**
  16. * Determine if the user is authorized to make this request.
  17. */
  18. public function authorize(): bool
  19. {
  20. return true;
  21. }
  22. public function rules(): array
  23. {
  24. return [
  25. 'name' => 'required|max:255',
  26. 'manager_id' => [
  27. 'nullable',
  28. Rule::exists('users','id')->where($this->userCompanyWhere()),
  29. ],
  30. 'parent_id' => [
  31. $this->parentIdExistsRule(),
  32. ]
  33. ];
  34. }
  35. protected function parentIdExistsRule()
  36. {
  37. // 如果 parent_id 不为 0,返回 exists 规则
  38. if ($this->input('parent_id') != 0) {
  39. return Rule::exists('department', 'id')->where($this->userCompanyWhere());
  40. }
  41. // 如果 parent_id 为 0,返回空数组以跳过 exists 验证
  42. return [];
  43. }
  44. }