I am pretty new to Laravel. With Laravel 11, I have this error which I couldn’t fix…
When I run:
AppModelsUser::factory()->create()
I get this error:
PARSE ERROR PHP Parse error: Unexpected character "" (ASCII 26) in vendorpsypsyshsrcExceptionParseErrorException.php on line 44.
This is my UserFactory.php
<?php
namespace DatabaseFactories;
use IlluminateDatabaseEloquentFactoriesFactory;
use IlluminateSupportFacadesHash;
use IlluminateSupportStr;
/**
* @extends IlluminateDatabaseEloquentFactoriesFactory<AppModelsUser>
*/
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,
]);
}
}
and User.php
<?php
namespace AppModels;
// use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
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',
];
}
}
I’ve tried:
- Checked for Hidden Characters
- Ensured that PHP files are saved with UTF-8 without BOM
- Checked composer
composer install
composer update
- Cleared cache
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
5.Another factory is working without problem
<?php
namespace DatabaseFactories;
use IlluminateDatabaseEloquentFactoriesFactory;
use IlluminateSupportFacadesHash;
use IlluminateSupportStr;
/**
* @extends IlluminateDatabaseEloquentFactoriesFactory<AppModelsRelease>
*/
class ReleaseFactory 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 [
'version_name' => fake()->firstName(),
'summary' => fake()->text(),
];
}
}
New contributor
Puka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.