0

TL/DR: I've got a many-to-many relationship between two models using withPivot and and using. I want to get the related data from the withPivot value from a foreign key.


I am working with a multi tenancy project, which has a master database and tenant databases.

Any model in the App\Tenant\ is currently using the $connection attribute.

I have the following structure of my models and their associated tables:

- App/Tenant/Match
- App/Tenant/MatchTeam
- App/Team
- App/Ground

The relationship between team and match is many-to-many

(a team can play multiple matches, and a match can have many teams)

Match.php

namespace App\Tenant
class Match extends TenantModel

public function teams() {
    return $this->belongsToMany(Team::class, 'tenant.match_team', 'match_id', 'team_uuid')
                ->using(MatchTeam::class)            
                ->withPivot('ground_id');
}

Team.php

namespace App
class Team extends Model

protected $primaryKey = 'uuid';

public $incrementing = false;

public function grounds() {
    return $this->hasMany(Ground::class, 'team_uuid', 'uuid');
}

public function matches() {
    return $this->belongsToMany(Match::class, 'tenant.match_team', 'team_uuid', 'match_id')
                ->using(MatchTeam::class)            
                ->withPivot('ground_id')
                ;
}

MatchTeam.php

class MatchTeam extends Pivot 

protected $connection = 'tenant';
protected $table = 'match_team';

public function ground() {
    return $this->hasOne(Ground::class);
}

match_team table:

| id |     team_uuid      | match_id | ground_id | 
|----| ------------------ | -------- | --------- |
| 1  | kajdnfgkasdnfadsgn |    1     |    NULL   |
| 2  | lsdjfgsadlkfjglsdj |    2     |     4     |
| 2  | kshdfkjdshfytufjek |    3     |     1     |

QUESTION:

How can I access the ground data that relates to the ground_id field on the match_team pivot table?

I'm primarily after something like the the match->ground->name which is a relationship on the foreign key on the match_team table.


I've tried the following:

MatchController.php

public function show(Match $match) {
    $match = Match::where('id', $match->id)->with('ground')->first();
}

But it gives Call to undefined relationship [ground]


DD($match) as requested below

Interaction {#215 ▼
#table: "matches"
#connection: "tenant"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:19 [▶]
#original: array:19 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
    "ground" => null
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
n8udd
  • 657
  • 1
  • 9
  • 30

1 Answers1

0

As Your error said there is No Ground Relation in the Match Model So in your Match Model

public function ground() {
    return $this->hasOne(Ground::class,'foreign_key', 'local_key');
}

EDITED

Since I don't know the table structure

MAY BE THIS WILL HELP YOU

METHOD ONE:

if(! is_null($match->pivot->ground))
{
  $groundName = $match->pivot->ground->name;
}else{
    $groundName = 'Ground Not Set';
}

METHOD TWO:

$groundName = $match->ground['name'] ??  'Ground Not Set';
ManojKiran A
  • 5,896
  • 4
  • 30
  • 43