You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel调用Artisan::call('cache:clear')时报错'Trying to access array offset...'

Fixing "Trying to access array offset on value of type int" When Clearing Cache via Route

Hey there! Let's break down why you're seeing this error and how to fix it.

What's Causing the Error?

The error pops up in the parse() method of Symfony's Console component (which Laravel's Artisan uses under the hood). This code expects parameter keys to be strings (like command options starting with -- or -), but instead, one of the keys is an integer. When it tries to access $key[0] (treating the integer like an array), PHP throws that "array offset on int" error.

This usually happens when the parameters passed to Artisan get messed up—either from a custom modification in your app, a minor version compatibility quirk, or an unexpected environment issue.

Solutions to Try

1. Explicitly Pass an Empty Parameter Array

Sometimes explicitly passing a blank associative array as the second argument to Artisan::call() fixes the parameter handling quirk:

Route::get('/clear-cache', function() { 
    Artisan::call('cache:clear', []); 
    return "Cache is cleared"; 
});

2. Execute the Command Directly via Shell

If the Artisan facade call is still acting up, you can run the command directly using PHP's shell execution functions. Just make sure your web server user has permission to run php artisan:

Route::get('/clear-cache', function() {
    $artisanPath = base_path('artisan');
    exec("php {$artisanPath} cache:clear");
    return "Cache is cleared";
});

3. Clear Config Cache Manually First

If your config cache is corrupted, it might interfere with Artisan's internal workings. SSH into your server and run this command first:

php artisan config:clear

Then try accessing your /clear-cache route again.

4. Check for Custom Console Modifications

Double-check if you (or a third-party package) have modified any of Laravel's Console classes or Symfony's Console components. Custom Input/Output classes or parameter parsers could be causing this unexpected behavior.

内容的提问来源于stack exchange,提问作者B L Praveen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:37:41