I have a model Team with has a polymorphic relationship with Marketcenters Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
public function teamable()
{
return $this->morphTo();
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Marketcenter extends Model
{
public function teams()
{
return $this->morphMany('App\Team', 'teamable');
}
}
I need to retieve all Teams for one or any Marketcenter so I can list all Teams and to which Marketcenter they belong. So I execute the following code and I get a collection od Teams for the Market Center in query:
$marketcenters = Marketcenter::where('id', $request->user()->marketcenter->id)->with('teams')->get();
foreach($marketcenters as $marketcenter) {
dd($marketcenter->teams);
}
But my problem appears when I want to retrieve each Team with their corresponing Market Center:
$marketcenters = Marketcenter::where('id', $request->user()->marketcenter->id)->with('teams')->get();
foreach($marketcenters as $marketcenter) {
dd($marketcenter->teams->marketcenter->mc_name);
}
Property [marketcenter] does not exist on this collection instance.
How can I retrieve parent data to child record in a Polymoprphic relationship?
Regards
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire