I have 3 models Company, Plan, and Service. What i'm trying to make is that each company should have plans 3-4 and each plan have it's own services that they provide.
Every company may have different plans and for those plans they may have different services.
I create a Many to Many Polymorphic relation for Plans called Planable, and this table will have companies's plans and services for these plans. But i was not able to retrieve the services for a specific company plans.
$company = Company::findOrFail($id);
foreach ($company->plans as $plan){
return $plan->services;
}
This is not working and it's only showing one service and it's separating each company with it's plans and services.
I would like to have make it like this $company->plans->services
Relations:
class Plan extends Model
{
public function companies(){
return $this->morphedByMany('App\Company', 'planable');
}
public function services(){
return $this->morphedByMany('App\Service', 'planable');
}
}
class Company extends Model
{
public function plans(){
return $this->morphToMany('App\Plan', 'planable');
}
}
class Service extends Model
{
public function plans(){
return $this->morphToMany('App\Plan', 'planable');
}
}