@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
@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?
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.
@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);
}
@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.