Laravel 11执行用户工厂创建时遇ASCII 26解析错误求助
Laravel 11 Tinker执行User工厂创建时的解析错误问题
在Laravel 11环境下,执行php artisan tinker并运行命令:
App\Models\User::factory()->create()
触发如下解析错误:
PARSE ERROR PHP Parse error: Unexpected character "" (ASCII 26) in vendor\psy\psysh\src\Exception\ParseErrorException.php on line 44.
相关代码文件
UserFactory.php
<?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> */ class UserFactory extends Factory { /** * The current password being used by the factory. */ protected static ?string $password; /** * Define the model's default state. * * @return array<string, mixed> */ public function definition(): array { return [ 'first_name' => fake()->firstName(), 'last_name' => fake()->lastName(), 'email' => fake()->unique()->safeEmail(), 'email_verified_at' => now(), 'password' => static::$password ??= Hash::make('password'), 'remember_token' => Str::random(10), ]; } /** * Indicate that the model's email address should be unverified. */ public function unverified(): static { return $this->state(fn (array $attributes) => [ 'email_verified_at' => null, ]); } }
User.php
<?php namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'first_name', 'last_name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array<string, string> */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', 'is_admin' => 'boolean', ]; } }
ParseErrorException.php(默认未修改)
<?php /* * This file is part of Psy Shell. * * (c) 2012-2023 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Psy\Exception; /** * A "parse error" Exception for Psy. */ class ParseErrorException extends \PhpParser\Error implements Exception { /** * Constructor! * * @param string $message (default: '') * @param array|int $attributes Attributes of node/token where error occurred * (or start line of error -- deprecated) */ public function __construct(string $message = '', $attributes = []) { $message = \sprintf('PHP Parse error: %s', $message); if (!\is_array($attributes)) { $attributes = ['startLine' => $attributes]; } parent::__construct($message, $attributes); } /** * Create a ParseErrorException from a PhpParser Error. * * @param \PhpParser\Error $e */ public static function fromParseError(\PhpParser\Error $e): self { return new self($e->getRawMessage(), $e->getAttributes()); } }
已尝试的解决方法
- 检查文件中的隐藏字符
- 确保PHP文件以UTF-8无BOM格式保存
- 执行Composer命令:
composer install composer update - 清除Laravel缓存:
php artisan cache:clear php artisan config:clear php artisan route:clear php artisan view:clear - 测试其他工厂(如ReleaseFactory)可正常运行,代码如下:
<?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Release> */ class ReleaseFactory extends Factory { /** * Define the model's default state. * * @return array<string, mixed> */ public function definition(): array { return [ 'version_name' => fake()->firstName(), 'summary' => fake()->text(), ]; } }
可能的解决方案
精准定位并删除ASCII 26字符
ASCII 26是SUB控制字符,肉眼不可见。可以用以下方法排查:- 使用VS Code开启"显示控制字符"功能(设置中搜索"Render Control Characters"),检查UserFactory.php、User.php以及PsySH的ParseErrorException.php文件,找到并删除该字符。
- 命令行搜索:Windows执行
findstr /r /c:"\x1A" app/Models/User.php database/factories/UserFactory.php vendor/psy/psysh/src/Exception/ParseErrorException.php;Linux/macOS执行grep -n $'\x1A' app/Models/User.php database/factories/UserFactory.php vendor/psy/psysh/src/Exception/ParseErrorException.php,定位到行后删除字符。
重新生成UserFactory文件
备份当前UserFactory.php后,执行php artisan make:factory UserFactory --model=User重新生成工厂框架,再手动迁移原有业务逻辑,复制代码时尽量避免从富文本编辑器粘贴,防止带入隐藏字符。彻底重置PsySH依赖
删除vendor目录和composer.lock文件,执行composer install重新安装所有依赖,确保PsySH的核心文件未被污染。脱离Tinker测试工厂
创建临时控制台命令或路由,直接执行App\Models\User::factory()->create(),验证是否是Tinker环境特有的问题。例如创建命令:php artisan make:command TestUserFactory在
handle方法中加入工厂执行代码,运行php artisan test:user-factory测试。
内容的提问来源于stack exchange,提问作者Puka
相关产品推荐
相关产品推荐

