Home > Blockchain >  Passing an id through a form to a foreign key
Passing an id through a form to a foreign key

Time:03-16

What I'm trying to do is create 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

Don't worry about the else at the beginning

CodePudding user response:

After creating Family you simply want to pass family_id to the student form and save the family_id in the student table (assuming family and student are two tables).

 $familly->save();
 return redirect()->route( 'familly.student.new' )->with( [ 'id' => $familly->id ] );

The reason I have written $familly->id is because in your migration id will be the primary key for family.

You also want to create a relationship between family and student (assuming family and student are two tables)to access the data cleanly. In your family model write

class Family extends Model
{
    /**
     * Get the student for the family.
     */
    public function students()
    {
        return $this->hasMany(Student::class);
    }
}

In your Student model

class Student extends Model
{
    /**
     * Get the family that has students.
     */
    public function family()
    {
        return $this->belongsTo(Family::class);
    }
}

Also, move the database part to the model.

CodePudding user response:

On the second else instead this line:

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

Try this:

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