Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

How can I use $this for the relationship object inside a query?

Hey guys!

Have a look at this query

$company->emailInvitations()
        ->where('email', $request->email)
        ->whereBetween('created_at', [$this->created_at->subMonth(), now()])
        ->exists()

How can I use $this for the relationship object?

0 likes
11 replies
Nakov's avatar

The relationship being the emailInvitations?

Then you can do this:

$company->emailInvitations()
        ->where('email', $request->email)
        ->whereHas('emailInvitations', function ($query) {
			$query->whereBetween('created_at', [now()->subMonth(), now()])
		})
        ->exists()

From what you are trying you will have to have the instance of the row already queried in order to try.

lat4732's avatar
Level 12

@Nakov

Call to undefined method App\Models\CompanyEmailInvitationsLog::emailInvitations() 

emailInvitations is not on the CompanyEmailInvitationsLog, it's on the Company model.

A Company hasMany CompanyEmailInvitationsLog

Nakov's avatar

@Laralex sorry that was a wrong query for me. I was trying to guess what you want to refer to with $this.

Can you give an example on what you are trying to achieve?

lat4732's avatar
Level 12

@Nakov As I said every company has many email invitations. I need to check if there is an invitation with a specific email address in the last month. I don't know what example to give you, in the first post it is the only example I can create. In the first post's example $this must refer to the emailInvitations relation

lat4732's avatar
Level 12

@Nakov Here's an example I can give you

$company->emailInvitations()
        ->where('email', $request->email)
        ->whereBetween('created_at', [
            $company->emailInvitations()
                    ->where('email', $request->email)
                    ->latest()
                    ->first()
                    ->value('created_at')
                    ->subMonth(), 
            now()
         ])
        ->exists()
Nakov's avatar
Nakov
Best Answer
Level 73

@Laralex okay, but you don't need $this then, you just need a month ago..

So this :

$company->whereHas('emailInvitations', function ($query) use ($request) {
	    $query->where('email', $request->email)
	        ->whereBetween('created_at', [now()->subMonth(), now()]);
	})
    ->exists()
lat4732's avatar
Level 12

@Nakov Very nice. Thanks :) Another quick question:

if($company->whereHas('emailInvitations', function ($query) use ($request) {
      $query->where('email', $request->email)
          ->whereBetween('created_at', [now()->subMonth(), now()]);
})->exists()) {

   return back()
           ->with('error', 'It must be 1 month before you send another invitation to this email. Time left: ' . $days_left);

}

How can I show the remaining days until the end of 1 month from the last invite?

lat4732's avatar
Level 12

@Nakov Okay, I'll try that.

But I have a problem with the last query you gave me. It's not working properly, because when I change the company profile and try to send a invitation to a email that another company sent to (email that exists in the email invitations log), it returns me an alert that it must be 1 month before my last invitation for this email.

Looks like it's not checking only for the certain company but the whole table.

Nakov's avatar

@Laralex you gave me $company above, so I don't know if that's new Company or a specific company.. why don't you do it the other way around then:

if(CompanyEmailInvitationsLog::where('company_id', $company->id)
->where('email', $request->email)
->whereBetween('created_at', '>', now()->subMonth())->exists()) {

   return back()
           ->with('error', 'It must be 1 month before you send another invitation to this email. Time left: ' . $days_left);

}
lat4732's avatar
Level 12

@Nakov Because I always keep in mind that relationships are more effective than this way. But yeah, this works perfectly by replacing whereBetween with where.

Please or to participate in this conversation.