vvbjvbvbvb
vvbjvbvbvb

Reputation: 3

Laravel: one to many relationship issue

I have two tables students and exams. I want to get the students who have exams as last exam date was before one year from today. How I can do this query with Laravel eloquent?

Upvotes: 0

Views: 30

Answers (1)

Mtxz
Mtxz

Reputation: 3869

$students = $students->whereDoesntHave( 'exams', function ( $query ) {
            $query->where( 'created_at', '>', Carbon::now()->startOfYear() );
        } )

This should query students who don't have exams created after the first day of the current year. I don't know if this is this date query that you want, but you get the idea.

Assuming:

  • relation is correctly defined and named

Upvotes: 1

Related Questions