我有一個多對多的關係來建立不返回結果,雖然有相關的數據。Laravel多對多關係
我在想什麼?
MySQL的架構:
entities
- id
services
- id
entity_service
- entity_id
- service_id
相關型號:
class Entity extends Eloquent implements UserInterface, RemindableInterface
{
// ...
public function services()
{
return $this->belongsToMany('Service');
}
}
class Service extends Eloquent
{
// ...
public function entities()
{
return $this->belongsToMany('Entity');
}
}
控制器/查看
$entity = Entity::findOrFail($id);
$locals['entity'] = $entity; // I can see all values available here
$locals['entity_services'] = $entity->services(); // I can't see any values here
@foreach ($entity_services as $service)
{{$service->id}}
@endforeach
如果您使用'$ this-> belongsToMany('Service','entity_service','entity_id','service_id');'? – Wrikken
沒有明顯的變化。 – Donnie
'Entity :: findOrFail($ id)'_does_ work,只是爲了確保? – Wrikken