Search code examples
phplaraveleloquentpivotmany-to-many

3 tables many to many relationship


I'm making a website in Laravel and ran into the following issue.

I have three tables: User, JobOffer, and Company Many users have many job offers.

I have created a ManyToMany-relationship between Users and Joboffers.

User model:

return $this->belongsToMany('App\JobOffer')->withTimestamps();

JobOffer model:

return $this->belongsToMany('App\User')->withTimestamps();

But the problem is the Joboffers table has a column company_id (because of the relationship between Company and Joboffer) and the relationship between Users-Joboffer returns the id of the company. But I would like to get the name of the company.

Thanks a lot!

Update:

My models:

App\User.php

public function job_offers()
{
    return $this->belongsToMany('App\JobOffer')->withTimestamps();
}

App\JobOffer.php

public function users()
{
    return $this->belongsToMany('App\User')->withTimestamps();
}

public function company()
{
    return $this->belongsTo('App\Company');
}

App\Company.php

public function job_offers()
{
    return $this->hasMany('App\JobOffer');
}

The user IGP was right. Thanks a lot IGP.


Solution

  • Until more details are provided, I'm assuming the relationships are as follows:

    • User model and JobOffer model have an M:N relationship
    • Company model and JobOffer model have a 1:M relationship
    # App\User.php
    public function job_offers()
    {
        return $this->belongsToMany('App\JobOffer')->withTimestamps();
    }
    
    # App\JobOffer.php
    public function users()
    {
        return $this->belongsToMany('App\User')->withTimestamps();
    }
    
    public function company()
    {
        return $this->belongsTo('App\Company');
    }
    
    # App\Company.php
    public function job_offers()
    {
        return $this->hasMany('App\JobOffer');
    }
    

    From these relationships you can get the company name like so:

    use App\User;
    
    $user = User::with('job_offers.company')->where(...)->first();
    

    The resulting object will look like this:

    App\User {
      id: 1,
      created_at: "",
      updated_at: "",
      job_offers: Illuminate\Database\Eloquent\Collection {
        all: [
          App\JobOffer {
            id: 85,
            company_id: 36,
            created_at: "",
            updated_at: "",
            pivot: Illuminate\Database\Eloquent\Relations\Pivot {
              job_offer_id: 85,
              user_id: 1,
              created_at: "",
              updated_at: "",
            },
            company: App\Company {
              id: 36,
              name: "Company1"
              created_at: "",
              updated_at: "",
            },
          },
          App\JobOffer {
            id: 90,
            company_id: 44,
            created_at: "",
            updated_at: "",
            pivot: Illuminate\Database\Eloquent\Relations\Pivot {
              job_offer_id: 90,
              user_id: 1,
              created_at: "",
              updated_at: "",
            },
            company: App\Company {
              id: 44,
              name: "Company2"
              created_at: "",
              updated_at: "",
            },
          },
        ],
      },
    }
    

    All you need to do is loop through the User's job offers to get each company name.

    @foreach($user->job_offers as $job_offer)
        Company name: {{ $job_offer->company->name }}
    @endforeach