多对多关系验证laravel

eqqqjvef  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(150)

我有三张table:

  • Users - courses - user_courses(多对多关系)
  • 用户[ id ]课程[ day - time - start_date - end_date ]
  • User_Course [user_id - course_id ]

我想验证用户不能在同一天注册两门课程,时间和期间从start_date到end_date这对于API我收到course_id请求,我已经记录了用户id任何帮助!Thx
我验证用户不能注册同一课程两次

fxnxkyjh

fxnxkyjh1#

您可以在表单请求中使用自定义验证规则来验证它。
要检查课程周期是否重叠,使用CarbonPeriodoverlaps方法非常简单。
因为我想不出有什么验证规则可以立即执行此操作,所以我使用验证器的after来注册一个回调方法。

多课程:

use Carbon\CarbonPeriod;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class EnrollRequest extends FormRequest
{
    protected $stopOnFirstFailure = true;

    public function rules(): array
    {
        return [
            'courses' => ['required', 'array'],
            'courses.*' => ['required', 'exists:courses,id'],
        ];
    }

    public function withValidator(Validator $validator): void
    {
        $user_courses = $this->user()->courses;

        $validator->after(function (Validator $validator) use ($user_courses) {
            $data = $validator->getData();
            $courses = Course::whereIn('id', $data['courses'])->get();

            foreach ($courses as $i => $course) {
                // check user hasn't enrolled in the course already
                if ($user_courses->contains($course) {
                    $validator->errors()->add("courses.$i", 'User is already enrolled in this course.');
                }

                // check if the course doesn't overlap with other courses the user has
                $course_period = CarbonPeriod::create($course->start_date, $course->end_date);
                foreach ($user_courses as $user_course) {
                    if ($couse_period->overlaps($user_course->start_date, $user_course->end_date)) {
                        $validator->errors()->add("courses.$i", 'Course overlaps.');
                    }
                }
            }
        });
    }
}
// controller
public function enroll(EnrollRequest $request)
{
    $request->user()->courses()->attach($request->validated()['courses']);
}

单课程

use Carbon\CarbonPeriod;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class EnrollRequest extends FormRequest
{
    protected $stopOnFirstFailure = true;

    public function rules(): array
    {
        return [
            'course_id' => ['required', 'exists:courses,id'],
        ];
    }

    public function withValidator(Validator $validator): void
    {
        $user_courses = $this->user()->courses;

        $validator->after(function (Validator $validator) use ($user_courses) {
            $data = $validator->getData();
            $course = Course::find($data['course_id']);

            // check user hasn't enrolled in the course already
            if ($user_courses->contains($course) {
                $validator->errors()->add("courses.$i", 'User is already enrolled in this course.');
            }

            // check if the course doesn't overlap with other courses the user has
            $course_period = CarbonPeriod::create($course->start_date, $course->end_date);
            foreach ($user_courses as $user_course) {
                if ($couse_period->overlaps($user_course->start_date, $user_course->end_date)) {
                    $validator->errors()->add('course_id', 'Course overlaps.');
                }
            }
        });
    }
}
// controller
public function enroll(EnrollRequest $request)
{
    $request->user()->courses()->attach($request->validated()['course_id']);
}

相关问题