samedi 6 mai 2017

Integrity constraint violation: 1048 Column > 'project_id' cannot be null

I'm new to Laravel.

I have ManyToMany relationship tables that are "customers" and "projects" table.

I want to create new records for these tables by DatabaseSeeder, but it fails as below.

What am I doing wrong???

[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'project_id' cannot be null (SQL: insert into orders (created_at, customer_id, project_id, updated_at) values (2017-05-06 23:00:11, 3, , 2017-05-06 23:00:11))

[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'project_id' cannot be null

DatabaseSeeder

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {


        Model::unguard();

        $this->call(BuildingsTableSeeder::class);
        $this->call(AgenciesTableSeeder::class);
        $this->call(BadgesTableSeeder::class);
        $this->call(ProductsTableSeeder::class);
        $this->call(StaffsTableSeeder::class);
        $this->call(RoomsTableSeeder::class);
        $this->call(CustomersTableSeeder::class);
        $this->call(ProjectsTableSeeder::class);
        //$this->call(OrdersTableSeeder::class);
        $this->call(VisitRecordsTableSeeder::class);

        Model::reguard();


    }

}

CustomersTableSeeder

class CustomersTableSeeder extends Seeder
{

    public function run()
    {

        DB::table("customers")->delete();

        $faker = Faker::create('ja_JP');



        for ($i = 0; $i < 10; $i++) {

            $building = Building::find($faker->numberBetween($min = 1, $max = 10));
            $room = Room::find($faker->numberBetween($min = 1, $max = 10));


            for ($i = 0; $i < 10; $i++) {

                $customer = new Customer([
                    'last_name' => $faker->lastName,
                    'first_name' => $faker->firstName,
                    'phone_number' => $faker->phoneNumber,
                    'sex' => $faker->randomElement($array = array("male","female")),
                    'memo' => $faker->text($maxNbChars = 100)

                ]);

                $customer->building()->associate($building);
                $customer->room()->associate($room);

                $customer->save();

            }

        }

    }

}

ProjectsTableSeeder

class ProjectsTableSeeder extends Seeder
{

    public function run()
    {

        DB::table("projects")->delete();

        $faker = Faker::create('ja_JP');


        for ($i = 0; $i < 10; $i++) {

            $product = Product::find($faker->numberBetween($min = 1, $max = 5));

            $customer = Customer::find($faker->numberBetween($min = 1, $max = 10));

            for ($i = 0; $i < 3; $i++) {

                $project = new Project([
                    'project_name' => $faker->company.$faker->name,
                    'project_value' => $faker->numberBetween($min = 1000, $max = 5000)
                ]);

                $project->product()->associate($product);
                $project->customers()->attach($customer->id);

                $project->save();

            }

        }


    }

}

Customer

class Customer extends Model
{

    public function projects()
    {

        return $this->belongsToMany("App\Project", "orders")->withTimestamps();

    }


}

Project

class Project extends Model
{


    public function customers()
    {

        return $this->belongsToMany("App\Customer", "orders")->withTimestamps();

    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire