运行 laravel 播种器时数组到字符串的转换错误
Array to string conversion error when running laravel seeders
我在 运行 播种器时遇到 ErrorException Array to string conversion
错误。
php artisan db:seed --class=StudentSeeder
我在StudentFactory.php
上修改之前工作正常,我只是改变了所有的伪造者,我不知道是哪个在制造麻烦。
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Student;
class StudentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Student::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'student_name' => $this->faker->name('male'),
'cpr' => $this->faker->creditCardNumber,
'email' => $this->faker->safeEmail,
'mobile' => $this->faker->e164PhoneNumber,
'mobile2' => $this->faker->e164PhoneNumber,
'dob' => $this->faker->date(),
'address' => $this->faker->address,
'live_inـstate' => $this->faker->randomElement(["UNKNOWN", "OWN", "RENT"]),
'relationshipـstate' => $this->faker->randomElement(["UNKNOWN", "SINGLE", "MARRIED", "DIVORCED"]),
'family_members' => $this->faker->randomNumber(),
'family_depends' => $this->faker->randomNumber(),
'degree' => $this->faker->text,
'hawzaـhistory' => $this->faker->boolean,
'hawzaـhistory_details' => $this->faker->paragraphs,
'healthـhistory' => $this->faker->boolean,
'healthـhistory_details' => $this->faker->paragraphs,
'financialـstate' => $this->faker->randomElement(["UNKNOWN", "POOR", "AVERAGE", "GOOD", "EXCELLENT"]),
'financial_details' => $this->faker->paragraphs,
'student_notes' => $this->faker->paragraphs,
'registration_at' => $this->faker->date(),
];
}
}
完整的控制台错误:
ErrorException
Array to string conversion
at vendor/laravel/framework/src/Illuminate/Support/Str.php:494
490▕
491▕ $result = array_shift($segments);
492▕
493▕ foreach ($segments as $segment) {
➜ 494▕ $result .= (array_shift($replace) ?? $search).$segment;
495▕ }
496▕
497▕ return $result;
498▕ }
+17 vendor frames
18 database/seeders/StudentSeeder.php:17
Illuminate\Database\Eloquent\Factories\Factory::create()
+22 vendor frames
41 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
将出现的所有 $this->faker->paragraphs
更改为 $this->faker->paragraphs(3, true)
。
paragraphs
格式化程序 outputs an array by default。第二个参数true
表示应该返回一个字符串而不是一个数组。
我在 运行 播种器时遇到 ErrorException Array to string conversion
错误。
php artisan db:seed --class=StudentSeeder
我在StudentFactory.php
上修改之前工作正常,我只是改变了所有的伪造者,我不知道是哪个在制造麻烦。
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Student;
class StudentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Student::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'student_name' => $this->faker->name('male'),
'cpr' => $this->faker->creditCardNumber,
'email' => $this->faker->safeEmail,
'mobile' => $this->faker->e164PhoneNumber,
'mobile2' => $this->faker->e164PhoneNumber,
'dob' => $this->faker->date(),
'address' => $this->faker->address,
'live_inـstate' => $this->faker->randomElement(["UNKNOWN", "OWN", "RENT"]),
'relationshipـstate' => $this->faker->randomElement(["UNKNOWN", "SINGLE", "MARRIED", "DIVORCED"]),
'family_members' => $this->faker->randomNumber(),
'family_depends' => $this->faker->randomNumber(),
'degree' => $this->faker->text,
'hawzaـhistory' => $this->faker->boolean,
'hawzaـhistory_details' => $this->faker->paragraphs,
'healthـhistory' => $this->faker->boolean,
'healthـhistory_details' => $this->faker->paragraphs,
'financialـstate' => $this->faker->randomElement(["UNKNOWN", "POOR", "AVERAGE", "GOOD", "EXCELLENT"]),
'financial_details' => $this->faker->paragraphs,
'student_notes' => $this->faker->paragraphs,
'registration_at' => $this->faker->date(),
];
}
}
完整的控制台错误:
ErrorException
Array to string conversion
at vendor/laravel/framework/src/Illuminate/Support/Str.php:494
490▕
491▕ $result = array_shift($segments);
492▕
493▕ foreach ($segments as $segment) {
➜ 494▕ $result .= (array_shift($replace) ?? $search).$segment;
495▕ }
496▕
497▕ return $result;
498▕ }
+17 vendor frames
18 database/seeders/StudentSeeder.php:17
Illuminate\Database\Eloquent\Factories\Factory::create()
+22 vendor frames
41 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
将出现的所有 $this->faker->paragraphs
更改为 $this->faker->paragraphs(3, true)
。
paragraphs
格式化程序 outputs an array by default。第二个参数true
表示应该返回一个字符串而不是一个数组。