因此,我创建了一个预订系统,您可以在其中创建地图并将其分配给活动。我有3个表events
,maps
和event_maps
在阅读了Laravel文档之后,我决定建立一个EmiratesToMany关系。
但是当我尝试通过事件模型来检索地图时,第一行只会得到一个。
在我的控制器中,我做
public function displayForm($id)
{
$event = EventModel::find($id);
print_r($event->maps);
}
结果是一个Illuminate \ Database \ Eloquent \ Collection对象,其中有2张地图中的最后一张,我一生都无法解决如何全部获得这些地图的问题。
我的事件模型
public function maps()
{
return $this->belongsToMany('App\Models\Booky\MapsModel',
// Table name of the relationship's joining table.
'event_maps',
// Foreign key name of the model on which you are defining the relationship
'map_id',
// Foreign key name of the model that you are joining to
'event_id'
);
}
我的MapsModel
public function event()
{
return $this->belongsToMany('App\Models\Booky\EventsModel',
// Table name of the relationship's joining table.
'event_maps',
// Foreign key name of the model on which you are defining the relationship
'event_id',
// Foreign key name of the model that you are joining to
'map_id'
);
}
数据库看起来像这样
events
- id
- lots of irrelevant data
maps
- id
- lots of irrelevant data
event_maps
- id
- event_id
- map_id
我当时在想也许我应该使用其他关系类型,但是据我了解,他们不使用像event_maps
这样的关系表。
其他所有操作都按预期进行。
任何人都可以清理这个烂摊子吗? :)
答案 0 :(得分:3)
Array
(
[Height] => 3/16
[Color] => Standard Red
[Material] => Die-cut, pressure-sensitive paper
[Package Quantity] => 1000/Pkg
[Reusable] => Yes
[Size] => 3/16 H x 1/4 W
)
的关系相反。试试这个:
id
并且:
public function maps()
{
return $this->belongsToMany('App\Models\Booky\MapsModel',
// Table name of the relationship's joining table.
'event_maps',
// Foreign key name of the model that you are joining to
'event_id'
// Foreign key name of the model on which you are defining the relationship
'map_id',
);
}