简体   繁体   中英

Eloquent query using join to retrieve pivot table and related data

In my controller I have a query like this:

$userinfos = User::with('skills');
$userinfos->with('positions');
$userinfos=$userinfos->get();

And that works fine, in my view, I get the position name like this:

@forelse ($userinfos[0]->positions as $key=>$position)
    {{ $position->name}}
@empty
    No position listed
@endforelse 

I have another like this to retrieve related company data:

$positions = \DB::table('positions')
->select(\DB::raw('position_user.id as id, positions.id as position_id, positions.name as position_name, companies.id as company_id, companies.name as company_name, position_user.user_id'))
->join('position_user','position_user.position_id','=','positions.id')
->join('companies','companies.id','=','position_user.company_id')
->join('users','users.id','=','position_user.user_id')
->where('position_user.user_id',$userid)
->get();

And that also works fine to get the positions and company name:

@forelse($positions as $key=>$position)
    {{$position->position_name}} @ {{$position->company_name}}
@empty
    No position listed
@endforelse

I am sure there must be a way to combine the two so that I can get the company name in the first query. I am looking for something like this:

$userinfos = User::with('skills');
$userinfos->with('positions');
$userinfos->whereHas('positions', function($thisQuery) 
{
    $thisQuery->join('companies','companies.id','=','position_user.company_id');
});

$userinfos=$userinfos->get();

Is that even possible? Any ideas/direction would be greatly appreciated!

EDIT:

Should I perhaps be looking at adding in a join to my Position class to enable the above:

public function users()
{
    return $this->belongsToMany('App\User')->withTimestamps()
    ->withPivot('company_id');
    ->join('companies','companies.id','=','position_user.company_id');
}

If so - how would I access that in the view?

You should probably create your own pivot model such as UserPosition, and override the newPivot() method.

class User extends Eloquent {

public function positions() { 
   return $this->belongsToMany('Position')->withPivot('company_id');
}

public function newPivot(Model $parent, array $attributes, $table, $exists) {

if ($parent instanceof Position) {
    return new UserPosition($parent, $attributes, $table, $exists);
}

    return parent::newPivot($parent, $attributes, $table, $exists);
}

}

And you'd have the pivot model such as

class UserPosition extends  Illuminate\Database\Eloquent\Relations\Pivot { 

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

Then you can do something like the code below. Of course you could iterate positions instead of fetching first().

$user = User::find(1);
$user->positions()->first()->pivot->company 

Please keep in mind I just wrote these as a guideline. Didn't test any of it. Hopefully you can apply the logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM