我有一个多对多的关系来建立不返回结果,虽然有相关的数据。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