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

theUnforgiven's avatar

User with many relationships, loop within Job class

Hey all,

I have a bunch of users that will all have or may not have have a tenancy (rental on a property) and is a belongsTo relationship.

So my question is, I've created a job that I need to check every day if they have a tenancy or not, then after 21 days it should fire of an email to admin user.

Any suggestions on how best to achieve this?

0 likes
11 replies
Cronix's avatar

In the job, I'd just create a query that

  1. gets users who do not have a tenency
  2. where last tenancy date = now()->subDays(21)

Then just build up your email(s) to admin based on the result(s) and queue it/them.

It really depends on how you store everything. You might just be able to use a has() on the belongsTo relationship and where last tenancy date is 21 days in the past.

1 like
theUnforgiven's avatar

@Cronix Thanks for a good shout, but I'm getting the following error now:

In HasRelationships.php line 555:

  Allowed memory size of 134217728 bytes exhausted (tried to allocate 266240
  bytes)

This is based on the following within the job class.

$user = User::has('tenancies')->get();
foreach($user as $u) {
            \Log::info($u);
}
36864's avatar

Are your users being logged? Can you determine if the error occurs before, during, or after fetching the users?

36864's avatar
36864
Best Answer
Level 13

Try to determine if it happens when handling a particular user or if it happens after a certain amount of users.

$user = User::has('tenancies')->inRandomOrder()->get();
foreach($user as $index => $u) {
    \Log::info($index . ' : ' . $u->id);
    \Log::info($u);
}
1 like
theUnforgiven's avatar

@36864 - I'm still getting:

local.ERROR: App\Jobs\CheckVerificationStatus has been attempted too many times or run too long. The job may have previously timed out.
36864's avatar

still

That's the first time you've mentioned that error in this thread.

The error is pretty self-descriptive, your job is currently marked as having too many tries. Clear your job queue and re-queue the job.

1 like
theUnforgiven's avatar

Still as in its still bytes exhausted sort of error.

Job queue is clear and failed jobs table is also clear now, so will try again,

theUnforgiven's avatar

Which now also seems to working, I can see the output within the log file.

36864's avatar

My post did nothing to help with your original question, it only helped you debug an unrelated issue. Please mark an accepted answer based on the topic of this thread so people who land here after searching for a related problem aren't confused.

theUnforgiven's avatar

Yes your post helped debug the issue, but also you mentioned the key => value within the loop so I see that as a valid answer.

Please or to participate in this conversation.