Home > Software engineering >  laravel 8 admin seeder dosn't seed filed email dosn't have a default value
laravel 8 admin seeder dosn't seed filed email dosn't have a default value

Time:05-18

i am trying to insert values to admin seeder but it dosn't seed i don't know why i wrote everything correct and my error is General error: 1364 Field 'email' doesn't have a default value") and i don't want to make default value i just want to seed the table to fill informations here is my table admin

public function up()
{
    Schema::create('admins', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->timestamps();
    });
}

and here is my model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use HasFactory;

    protected $fillable = [
        'name','email','password'
    ];
}

and here is my created AdminSedder.php

<?php

namespace Database\Seeders;

use App\Models\Admin;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class AdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('admins')->delete();

        $infos = [
            ['name' => 'john'],
            ['email' => '[email protected]'],
            ['password' => Hash::make('12345678')],
        ];

        foreach($infos as $info){
            Admin::insert($info);
        }
    }
}

and my DatabaseSeeder

public function run()
{
    
    $this->call(AdminSeeder::class);
}

CodePudding user response:

You are passing only the name as value for the seeder. Try this instead:

$infos = [
    [
        'name' => 'john',
        'email' => '[email protected]',
        'password' => Hash::make('12345678'),
    ],
];       

And if you need more seeds, make sure that the email is unique

CodePudding user response:

It looks like your infos array is invalid. In nested array all elements should be in a same array.

$infos = [
    [
        'name' => 'john',
        'email' => '[email protected]',
        'password' => Hash::make('12345678'),
    ]
];

CodePudding user response:

In your AdminSeeder.php try it with the Admin model then import the two classes use App\Models\Admin; and use Faker\Factory as Faker;

 public function run()
    {
        $faker = Faker::create();

        Admin::create([
            'name'      =>  'john',
            'email'     =>  '[email protected]',
            'password'  => Hash::make('12345678'),
        ]);
    }
  • Related