At the top of your script add this:
ini_set('max_execution_time', 0);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm running a command on one of my websites
php artisan command:here
after a few minutes I get this status "Timed Out", is there any way to increase something to prevent the time out?
subscriptions table has 40,000 records and incomes table has 8,000,000. Every subscription has a maximum of 200 records in the incomes table.
Subscription.php
public function income()
{
return $this->hasMany(Income::class);
}
Income.php
public function subscription()
{
return $this->belongsTo(Subscription::class);
}
Code of the command I want to run
foreach (Subscription::where('status', 'ACTIVE')->lazy() as $subscription) {
$count_earnings = $subscription->income()->count();
$recent_bonus = $subscription->income()->latest('id')->first();
if ($recent_bonus) {
if ($count_earnings < 200) {
$hour_difference = now()->diffInHours($recent_bonus->created_at);
if ($hour_difference > 1) {
$to_insert = 200 - $count_earnings;
$max = $hour_difference;
if ($hour_difference > $to_insert) {
$max = $to_insert;
}
for ($i = 0; $i < $max; $i++) {
$income = new Income;
$income->user_id = $subscription->user_id;
$income->subscription_id = $subscription->id;
$income->amount = (100 * 0.002) * 100;
$income->save();
}
if (($count_earnings + $max) >= 200) {
$subscription->update(['status' => 'COMPLETED']);
}
Log::info('Fix for:'.$subscription->id.' | User:'.$subscription->user_id.' | Total:'.$max);
}
} else {
$subscription->update(['status' => 'COMPLETED']);
}
}
}
@benjamin1509 Here are couple of improvements, because 40.000 are not a lot of records:
in Subscription.php add this:
public function latestIncome()
{
return $this->hasOne(Income::class)->latestOfMany();
}
then in your script:
// replace this
foreach (Subscription::where('status', 'ACTIVE')->lazy() as $subscription) {
$count_earnings = $subscription->income()->count();
$recent_bonus = $subscription->income()->latest('id')->first();
// with:
foreach (Subscription::with('latestIncome')->withCount('income')->where('status', 'ACTIVE')->lazy() as $subscription) {
$count_earnings = $subscription->income_count;
$recent_bonus = $subscription->latestIncome;
Obviously test it locally first.
Please or to participate in this conversation.