Self-relationship with Eloquent in Laravel 5.3 [duplicate]

2

I am building an immigrant control application and own an imigrants table that has 3 auto relationships. Its structure is as follows.

  • id - > pk
  • first_name - > varchar
  • last_name - > varchar
  • (...)
  • father - > integer (fk)
  • mother - > integer (fk)
  • spouse - > integer (fk)

But I can not determine the right relationship with eloquent (I'm still learning to use laravel). I have already tested with hasOne, belongsTo etc. and I can not make it associate properly. When using for example the query:

$imigrant->father()->save($father)

Added to the $ imigrant id in $ father. Contrary to what I desire. And if you use belongsTo() with associate() , eg:

$imigrant->father()->associate($father)

He arrow null in the $ imigrant id and makes a merge of $ father getting type:

App\Imigrant {#680
 id: null,
 first_name: "FName",
 last_name: "LName",
 birth_place: null,
 birth_date: null,
 disembark_place: null,
 disembark_date: null,
 father: App\Imigrant {#676
   id: 1,
   first_name: "FName2",
   last_name: "LName2",
   birth_place: null,
   birth_date: null,
   disembark_place: null,
   disembark_date: null,
   father: null,
   mother: null,
   spouse: null,
   ship: null,
   created_at: "2016-12-26 14:59:17",
   updated_at: "2016-12-26 14:59:17",
 },
 mother: null,
 spouse: null,
 ship: null,
 created_at: "2016-12-26 15:12:16",
 updated_at: "2016-12-26 15:12:16",

And it does not update the database.

I know I could do the manual association, but I wanted to use the eager query features that associate the data and automatically pull the associations in the search. If someone can give me a light on how I could do the association in the Model to make it work. Otherwise, I'll have to make do with my nails. : D

    
asked by anonymous 26.12.2016 / 17:45

1 answer

2

After breaking the head a lot I decided. Besides I forgot to restart the tinker (my changes were not reflecting). The correct association for my case was $this->belongsTo() because the FK is local (it's in the same table).

Thanks for the answers, it helped a lot: D

Here's how the Model worked (at first):

class Imigrant extends Model
{
    protected $genders = [
        "m" => "masculino",
        "f" => "feminino"
    ];

    protected $fillable = [
        "first_name",
        "last_name",
        "gender"
    ];

    protected $dates = [
        "created_at",
        "updated_at",
        "birth_date",
        "disembard_date"
    ];

    public function father()
    {
        return $this->belongsTo("App\Imigrant", "father_id");
    }

    public function mother()
    {
        return $this->belongsTo("App\Imigrant", "mother_id");
    }

    public function spouse()
    {
        return $this->belongsTo("App\Imigrant", "spouse_id");
    }

    public function documents()
    {
        return $this->hasMany("App\Document");
    }

    public function contributions()
    {
        return $this->hasMany("App\Contribution");
    }

    public function children()
    {
        return $this->hasMany("App\Imigrant")->where("father_id", $this->id);
    }

    public function associateFather(Imigrant $father)
    {
        $r = $father->save();
        $this->father()->associate($father);
        return $this->save() == $r;
    }

    public function associateMother(Imigrant $mother)
    {
        $r = $mother->save();
        $this->mother()->associate($mother);
        return $this->save() == $r;
    }

    public function associateSpouse(Imigrant $spouse)
    {
        $spouse->spouse_id = $this->id;
        $r = $spouse->save();
        $this->spouse()->associate($spouse);
        return $this->save() == $r;
    }

    public function getGenderAttribute($value)
    {
        if ($this->gender == "Masculino") {
            return $this->hasMany("App\Imigrant", "father_id");
        } else {
            return $this->hasMany("App\Imigrant", "mother_id");
        }
    }
}
    
27.12.2016 / 17:40