Laravel 8 播种时日期时间无效
Laravel 8 Invalid Date time while seeding
我是 laravel 的新手,我在尝试播种数据时收到错误,我已经在两个不同的表中成功完成了,但我被困在这个表中:
型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'Pmid',
'Ministry',
'P_name',
'Budget',
];
protected $casts = [
'Registered_at' => 'datetime',
];
}
工厂
<?php
namespace Database\Factories;
use App\Models\Project;
use Illuminate\Database\Eloquent\Factories\Factory;
class ProjectFactory extends Factory
{
protected $model = Project::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'Pmid' => $this->faker->unique()->name(),
'Ministry' => $this->faker->name(),
'P_name' => $this->faker->name(),
'Budget' => $this->faker->name(),
'Registered_at' => now(),
];
}
}
播种机
<?php
namespace Database\Seeders;
use App\Models\Project;
use Illuminate\Database\Seeder;
class ProjectTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Project::factory()->count(20)->create();
}
}
迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('Pmid')->unique();
$table->string('Ministry');
$table->string('P_name');
$table->integer('Budget');
$table->timestamp('Registered_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('projects');
}
}
错误
Illuminate\Database\QueryException
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Mathias Kuhlman' for column laravel
.projects
.Budget
at row 1 (SQL: insert into projects
(Pmid
, Ministry
, P_name
, Budget
, Registered_at
) values (Nicholas Mayer, Ms. Donna Strosin, Hermann Bins, Mathias Kuhlman, 2021-12-05 08:36:39))
您正在使用
'Budget' => $this->faker->name(),
创建数据,但您的迁移显示该列应该是一个整数:
$table->integer('Budget');
我怀疑您只是复制并粘贴了伪造的行,而没有更改被伪造的值的类型。
我是 laravel 的新手,我在尝试播种数据时收到错误,我已经在两个不同的表中成功完成了,但我被困在这个表中: 型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'Pmid',
'Ministry',
'P_name',
'Budget',
];
protected $casts = [
'Registered_at' => 'datetime',
];
}
工厂
<?php
namespace Database\Factories;
use App\Models\Project;
use Illuminate\Database\Eloquent\Factories\Factory;
class ProjectFactory extends Factory
{
protected $model = Project::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'Pmid' => $this->faker->unique()->name(),
'Ministry' => $this->faker->name(),
'P_name' => $this->faker->name(),
'Budget' => $this->faker->name(),
'Registered_at' => now(),
];
}
}
播种机
<?php
namespace Database\Seeders;
use App\Models\Project;
use Illuminate\Database\Seeder;
class ProjectTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Project::factory()->count(20)->create();
}
}
迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('Pmid')->unique();
$table->string('Ministry');
$table->string('P_name');
$table->integer('Budget');
$table->timestamp('Registered_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('projects');
}
}
错误
Illuminate\Database\QueryException SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Mathias Kuhlman' for column
laravel
.projects
.Budget
at row 1 (SQL: insert intoprojects
(Pmid
,Ministry
,P_name
,Budget
,Registered_at
) values (Nicholas Mayer, Ms. Donna Strosin, Hermann Bins, Mathias Kuhlman, 2021-12-05 08:36:39))
您正在使用
'Budget' => $this->faker->name(),
创建数据,但您的迁移显示该列应该是一个整数:
$table->integer('Budget');
我怀疑您只是复制并粘贴了伪造的行,而没有更改被伪造的值的类型。