CLI命令结果导出CSV遇maatwebsite/excel安装失败,求替代方案
问题场景
执行Laravel CLI命令php artisan select:values --year=2020 --month=02后,需要将输出结果保存到本地文件。尝试安装maatwebsite/excel时遇到依赖冲突错误:
Your requirements could not be resolved to an installable set of packages. Problem 1 - maatwebsite/excel[3.1.0, ..., 3.1.25] require php ^7.0 -> your php version (8.1.2) does not satisfy that requirement. - maatwebsite/excel[3.1.26, ..., 3.1.35] require illuminate/support 5.8.*|^6.0|^7.0|^8.0 -> found illuminate/support[v5.8.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require. - phpoffice/phpspreadsheet[1.18.0, ..., 1.25.2] require ext-gd * -> it is missing from your system. Install or enable PHP's gd extension. - maatwebsite/excel[3.1.41, ..., 3.1.x-dev] require psr/simple-cache ^1.0|^2.0 -> found psr/simple-cache[1.0.0, 1.0.1, 2.0.0, 2.x-dev] but the package is fixed to 3.0.0 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command. - maatwebsite/excel[3.1.36, ..., 3.1.40] require phpoffice/phpspreadsheet ^1.18 -> satisfiable by phpoffice/phpspreadsheet[1.18.0, ..., 1.25.2]. - Root composer.json requires maatwebsite/excel ^3.1 -> satisfiable by maatwebsite/excel[3.1.0, ..., 3.1.x-dev]. To enable extensions, verify that they are enabled in your .ini files: - /etc/php/8.1/cli/php.ini - /etc/php/8.1/cli/conf.d/10-mysqlnd.ini - /etc/php/8.1/cli/conf.d/10-opcache.ini - /etc/php/8.1/cli/conf.d/10-pdo.ini - /etc/php/8.1/cli/conf.d/15-xml.ini - /etc/php/8.1/cli/conf.d/20-calendar.ini - /etc/php/8.1/cli/conf.d/20-ctype.ini - /etc/php/8.1/cli/conf.d/20-curl.ini - /etc/php/8.1/cli/conf.d/20-dom.ini - /etc/php/8.1/cli/conf.d/20-exif.ini - /etc/php/8.1/cli/conf.d/20-ffi.ini - /etc/php/8.1/cli/conf.d/20-fileinfo.ini - /etc/php/8.1/cli/conf.d/20-ftp.ini - /etc/php/8.1/cli/conf.d/20-gettext.ini - /etc/php/8.1/cli/conf.d/20-iconv.ini - /etc/php/8.1/cli/conf.d/20-mysqli.ini - /etc/php/8.1/cli/conf.d/20-pdo_mysql.ini - /etc/php/8.1/cli/conf.d/20-phar.ini - /etc/php/8.1/cli/conf.d/20-posix.ini - /etc/php/8.1/cli/conf.d/20-readline.ini - /etc/php/8.1/cli/conf.d/20-shmop.ini - /etc/php/8.1/cli/conf.d/20-simplexml.ini - /etc/php/8.1/cli/conf.d/20-sockets.ini - /etc/php/8.1/cli/conf.d/20-sysvmsg.ini - /etc/php/8.1/cli/conf.d/20-sysvsem.ini - /etc/php/8.1/cli/conf.d/20-sysvshm.ini - /etc/php/8.1/cli/conf.d/20-tokenizer.ini - /etc/php/8.1/cli/conf.d/20-xmlreader.ini - /etc/php/8.1/cli/conf.d/20-xmlwriter.ini - /etc/php/8.1/cli/conf.d/20-xsl.ini You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode. Alternatively, you can run Composer with `--ignore-platform-req=ext-gd` to temporarily ignore these required extensions. Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions. You can also try re-running composer require with an explicit version constraint, e.g. "composer require maatwebsite/excel:*" to figure out if any version is installable, or "composer require maatwebsite/excel:^2.1" if you know which you need. Installation failed, reverting ./composer.json and ./composer.lock to their original content.
替代解决方案
方案1:Shell重定向输出(无需额外依赖)
直接通过命令行重定向功能,将命令输出写入本地文件:
- 保存为纯文本文件:
php artisan select:values --year=2020 --month=02 > output.txt
- 同时保存标准输出和错误输出:
php artisan select:values --year=2020 --month=02 > output.txt 2>&1
- 追加内容到已有文件:
php artisan select:values --year=2020 --month=02 >> output.txt
方案2:修改自定义Artisan命令,直接导出结构化文件
如果select:values是你自定义的命令,可修改其handle()方法,直接将查询结果导出为CSV或JSON:
导出CSV示例
public function handle() { // 替换为你的数据获取逻辑 $results = YourModel::whereYear('created_at', $this->option('year')) ->whereMonth('created_at', $this->option('month')) ->get(); $filePath = storage_path('app/values_' . $this->option('year') . $this->option('month') . '.csv'); $file = fopen($filePath, 'w'); // 写入表头 if (!$results->isEmpty()) { fputcsv($file, array_keys($results->first()->toArray())); } // 写入数据行 foreach ($results as $row) { fputcsv($file, $row->toArray()); } fclose($file); $this->info('文件已保存至: ' . $filePath); }
导出JSON示例
public function handle() { // 替换为你的数据获取逻辑 $results = YourModel::whereYear('created_at', $this->option('year')) ->whereMonth('created_at', $this->option('month')) ->get(); $filePath = storage_path('app/values_' . $this->option('year') . $this->option('month') . '.json'); file_put_contents($filePath, $results->toJson(JSON_PRETTY_PRINT)); $this->info('文件已保存至: ' . $filePath); }
方案3:PHP临时处理命令输出(无需修改命令代码)
若命令输出为JSON格式,可通过管道结合PHP内置函数将输出转为CSV:
php artisan select:values --year=2020 --month=02 | php -r '$data = json_decode(stream_get_contents(STDIN), true); if (!empty($data)) { $fp = fopen("output.csv", "w"); fputcsv($fp, array_keys($data[0])); foreach($data as $row) fputcsv($fp, $row); fclose($fp); }'
注:若原命令输出为纯文本,需根据输出格式调整PHP处理逻辑。
内容的提问来源于stack exchange,提问作者Yorkata008
相关产品推荐
相关产品推荐

