mercredi 27 décembre 2017

unable to make the foreign key get its referenced id value by default

i'am new in LARAVEL and i got some issues .

My problem is that when i insert data into the first table i can see that it has an id setted by default but when i insert data into the second table i keep getting this error :

SQLSTATE[HY000]: General error: 1364 Field 'etudiant_id' doesn't have a default value

what should i do to give the foreign key the id number of the first table ?

Here is my Schemas :

the first table -etudiant- :

public function up()
{
    Schema::create('etudiants', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nom');
        $table->timestamps();
    });
}

the second table -bac2- :

public function up()
{
    Schema::create('bac2', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('etudiant_id')->unsigned();
        $table->foreign('etudiant_id')->references('id')->on('etudiants')-
        >onDelete('cascade') ;
        $table->date('anne_bac2');
        $table->timestamps();

    });
}

Here is my insertion function :

function insert(Request $req){

     $nom = $req->input('name');

     $data= array('nom'=>$nom);

     $anne_bac2 = $req->input('anne_bac2'); 

     $data2= array('anne_bac2'=>$anne_bac2);


     DB::table('etudiants')->insert($data);
     DB::table('bac2')->insert($data2);

return "success";

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2CcbhIx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire