there is a lesson model

public function teacher() { return $this->hasMany(LessonTeacher::class, 'lesson_id'); } 

And a model with LessonTeacher tutors

  public function lesson() { return $this->belongsTo(Lesson::class,'id'); } 

I enter the date and the teacher in the form and send an AJAX request to the controller. I want to know if the teacher is busy on the date the user entered into the form. There are no problems with the date, but there is an error with the teacher search

 Method Illuminate\Database\Eloquent\Collection::teacher does not exist 

Request itself

 $check_lesson_date = Lesson::where(function($query) use ($date_start) { return $query->where('date_start', '<=', $date_start)->where('date_end', '>=', $date_start); }) ->orWhere(function($query) use ($date_end) { return $query->where('date_start', '<=', $date_end)->where('date_end', '>=', $date_end); }) ->get(); $check_teacher = $check_lesson_date->teacher()->where('teacher', $teacher)->count(); 

Tell me what I'm doing wrong and how to make a request.

    1 answer 1

    You can use the whereHas method, but now it turns out you are taking a collection and trying to refer to it from the collection.

     $teacherName = ''; // Ваша переменная $teacher. $check_lesson_date = Lesson::whereHas('teacher', function ($query) use ($teacher) { $query->where('teacher', $teacher); })->where(function($query) use ($date_start) { return $query->where('date_start', '<=', $date_start)->where('date_end', '>=', $date_start); }) ->orWhere(function($query) use ($date_end) { return $query->where('date_start', '<=', $date_end)->where('date_end', '>=', $date_end); }) ->exists(); 
    • Thank you, I understood, it helped) Only now it returns undefined, but that's another story) - BorsheC