Home > database >  Laravel 8 Invalid Date time while seeding
Laravel 8 Invalid Date time while seeding

Time:12-06

I am new to laravel , I receive error while trying to seed data, I have done it successfully in two different tables but I am stuck at this one : Model

<?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',
    ];
}

Factory

<?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(),
        ];
    }
}

Seeder

<?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();
    }
}

Migration

<?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');
    }
}

error

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))

CodePudding user response:

You're using

'Budget'        => $this->faker->name(),

to create the data, but your migration shows that that column should be an integer :

$table->integer('Budget');

I suspect you've just copied and pasted the faker rows without changing the type of value being faked.

  • Related