infernobass7's avatar

Laravel + MongoDB Relationships

I asked the following on github but I wanted to see if I could get some additional eyes on this issue.

I have been trying pretty much all day to get relationships to work in my application. I have tried many different combinations of relationships to get this to work.

Class Job {

    public function contractor() {
        return $this->hasOne('App\Company','job_id','contractor_id');
    }

    public function architect() {
        return $this->hasOne('App\Company','job_id','architect_id');
    }
}
Class Company {

    public function jobContractor() {
        return $this->belongsTo('App\Job','job_id','contractor_id');
    }

    public function jobArchitect() {
        return $this->belongsTo('App\Job','job_id','architect_id');
    }

}

The company class I have tried like this too:

Class Company {

    public function job() {
        return $this->belongsTo('App\Job','job_id','contractor_id');
    }

}

The business rules are:

A job can have one architect and one contractor A company can be an architect or contractor for more than one job I need the architect and contractor's ID able to be different. If I can get anything to work, it will work in one direction and it will not save to the database.

Am I doing something wrong or missing something?

0 likes
2 replies
infernobass7's avatar

I figured out my issue. The job needed to belong to the company,which doesn't make too much sense to me for the application. Also, is there a way to add a new relationship from the job class to the company by saving on the job instead of on the company?

Please or to participate in this conversation.