Home > OS >  LARAVEL : Passing an id through a form to a foreign key
LARAVEL : Passing an id through a form to a foreign key

Time:03-01

I'm a beginner with Laravel and I'm a little bit confused with something. So what I'm trying to do is creating a family through a form, then save this family and after that we are redirect to a minorstudent form where you can create a student that is link to the family you just created before.

Everything on my models, migrations is okay because in my controller I can see the student and the family, but my problems is when I create the family I want to pass the 'id' of the table 'familly' to the table family which has in its table the foreign key 'familly_id' ...But I don't know how to do it on my controllers. Thanks for the future help.

else {
          $minorstudent = new Minorstudent;
          $minorstudent->first_name = $request->first_name;
          $minorstudent->last_name = $request->last_name;
          $minorstudent->phone_number = $request->phone_number;
          $minorstudent->street_address = $request->street_address;
          $minorstudent->postal_code = $request->postal_code;
          $minorstudent->city = $request->city;
          $minorstudent->email_address = $request->email_address;
          $minorstudent->level = $request->level;
          if ($request->school!="none") {
              $minorstudent->school()->associate($request->school);
          }
          $minorstudent->save();
          return redirect('/familly');
        }

And I want the 'id' of the family I created before being pass to the minorstudent into 'familly_id' which is a foreign key.

 else {
        $minorstudent = Minorstudent::where('id',$request->id)->first();
        if ( $request->get('add_student_next') !== null ) {
          $familly = new Familly;
          $familly->first_name = $request->first_name;
          $familly->last_name = $request->last_name;
          $familly->phone_number = $request->phone_number;
          $familly->street_address = $request->street_address;
          $familly->postal_code = $request->postal_code;
          $familly->city = $request->city;
          $familly->email_address = $request->email_address;
          $familly->absence = 1;
          $familly->rules = 1;
          $minorstudent->familly_id()->attach($familly); 
          $familly->save();
          return redirect("/familly/id/student/new");
        }
        

This the family controllers (form) where you create the family and after that you are redirect to the minorstudent form

ps: Don't worry about the else at the beginning

CodePudding user response:

On the second else instead this line:

  $minorstudent->familly_id()->attach($familly); 

Try this:

$minorstudent->update(['familly_id' => $family->id]);

CodePudding user response:

"Call to a member function update() on null "

In my redirect I need to write something :

return redirect("/familly/id/student/new"); because it redirect me on my form but not with the "id" of the familly I just created before like "/familly/1/student/new" "/familly/2/student/new" ...

To be clear the first form is "newfamilly" link to the controllers I sent before.And the second form is "newminorstudent".

On the "newminorstudent" bladeview , I use a form action

//form action="{{ route('addchild',['id']) }}" method="POST"//

But Im sure I have to write something instead of ['id'].

Minorstudent migrations

 Schema::create('minorstudents', function (Blueprint $table) {
          $table->id();
          $table->unsignedBigInteger('familly_id');
          $table->foreign('familly_id')->references('id')->on('famillies');
          $table->string("first_name");
          $table->string("last_name");
          $table->string("email_address");
          $table->string("phone_number");
          $table->string("street_address");
          $table->string("city");
          $table->string("postal_code");
          $table->integer("school_id")->nullable();
          $table->string("level");
          $table->enum('choices', ['Paiement en attente', 'Paiement effectué'])->default('Paiement en attente');    
          $table->timestamps();
        });
        
        
        Familly migration

 Schema::create('famillies', function (Blueprint $table) {
        $table->id();
            $table->timestamps();
            $table->string("first_name");
            $table->string("last_name");
            $table->string("email_address");
            $table->string("phone_number")->nullable();
            $table->string("street_address");
            $table->string("city");
            $table->string("postal_code");
            $table->boolean("absence");  
            $table->boolean("rules");
            }); 
        //
        
        They are not on the same controller file.
        
       

  • Related