SQLSTATE [23000]: Integrity constraint violation: 1452 Can not add or update a child row laravel

0

Well folks, even though I checked several answers in the forum, I still can not solve my problem.

Schema::create('alunos', function (Blueprint $table) {
            $table->integer('id_academia')->unsigned();
            $table->increments('id_aluno');
            $table->string('id_aluno_academia', 3);
            $table->string('foto')->nullable();
            $table->string('nome');
            $table->string('email', 70);
            $table->string('celular', 16);
            $table->string('telefone', 16)->nullable();
            $table->date('data_nasc');
            $table->string('idade', 2);
            $table->string('cpf', 14);
            $table->string('rg', 12)->nullable();
            $table->boolean('sexo');
            $table->string('cep')->nullable();
            $table->string('endereco');
            $table->string('num_end');
            $table->string('bairro');
            $table->string('cidade', 50);
            $table->string('uf', 2);
            $table->string('plano', 1)->nullable();
            $table->string('tipo_pagamento', 1)->nullable();
            $table->string('turma', 3)->nullable();
            $table->string('professor', 3)->nullable();
            $table->string('senha')->nullable();
            $table->string('status', 1)->default(0);
            $table->date('expira')->nullable();
            $table->boolean('acesso')->default(0);
            $table->string('tipo_visita', 1)->nullable();
            $table->string('indicado_por', 1)->nullable();
            $table->string('modalidades_interesse')->nullable();
            $table->string('nivel_interesse')->nullable();
            $table->string('objetivo')->nullable();
            $table->text('observacoes')->nullable();
            $table->timestamps();
            $table->foreign('id_academia')->references('id_academia')->on('academias')->onDelete('cascade');
        });

And my seeder:

public function run()
    {
        $nomes = 
        [
            'Felipe', 'João', 'Gabriela', 'Miguel', 'Renata', 'Juliana', 'Sintia', 'Marcos',
            'Débora', 'Diego', 'Carlos', 'Fabiano', 'Maria', 'Jurema', 'Joaquim', 'Artur',
            'Eduarda', 'Antonella', 'Julieta', 'Ema'
        ];

        $sobrenomes = 
        [
            'Silva', 'Santos', 'Paz', 'Guedes', 'Silveira', 'Ribeiro', 'Magalhães', 'Garcia',
            'Agostini', 'Goldmann', 'Schulz', 'Schmidt', 'Bellini'
        ];

        $ufs = 
        [
            'AC','AL','AM','AP','BA','CE','DF','ES','GO','MA','MG','MS','MT',
            'PA','PB','PE','PI','PR','RJ','RN','RO','RR','RS','SC','SE','SP','TO'
        ];

        $emails =
        [
            '@gmail.com', '@outlook.com', '@yahoo.com', '@uol.com.br', '@msn.com', '@bol.com.br'
        ];

        $alunos = [];

        for ($i=1; $i < 2; $i++) { 

            for ($c=0; $c < 14; $c++) { 
                $cpf[$c] = rand(0,9);
            }

            for ($r=0; $r < 10; $r++) { 
                $rg[$r] = rand(0,9);
            }

            for ($n=0; $n < 3; $n++) { 
                $num[$n] = rand(0,9);
            }

            $dia = rand(1, 26);
            $mes = rand(1, 12);
            $ano = rand(2012, 2017);

            $nome = $nomes[array_rand($nomes)];
            $sobrenome = $sobrenomes[array_rand($sobrenomes)];
            $email = strtolower($nome . '.' . $sobrenome).$emails[array_rand($emails)];

            $expira = $ano . '-' . $mes . '-' . $dia;
            $sexo = rand(0,1);

            $alunos =
            [
                'id_academia'         => rand(1, 10),
                'id_aluno_academia'   => $i,
                'nome'                => $nome.' '.$sobrenome,
                'email'               => $email,
                'celular'             => '(54) 9 9945-2428',
                'data_nasc'           => '1989-09-01',
                'idade'               => rand(18, 45),
                'cpf'                 => implode('',$cpf),
                'rg'                  => implode('', $rg),
                'sexo'                => $sexo,
                'endereco'            => 'Rua Maratona',
                'num_end'             => implode('', $num),
                'bairro'              => 'Vila Isabel',
                'cidade'              => 'Passo Fundo',
                'uf'                  => $ufs[array_rand($ufs)],
                'plano'               => rand(1,4),
                'senha'               => md5(time())
            ];

            DB::table('alunos')->insert($alunos);
        }
        // $alunos->save();
    }

It sounds like a joke, but yesterday it worked correctly, today, I can not seem to identify the error.

  

[PDOException]
    SQLSTATE [23000]: Integrity constraint violation: 1452 Can not add or update child row: a foreign key constraint fails ( gymdoctor , alunos , CONSTRAINT alunos_id
_academia_foreign
FOREIGN KEY ( id_academia ) REFERENCES academias %) ON DELETE CASCADE)

    
asked by anonymous 23.11.2017 / 07:44

0 answers