【发布时间】:2016-11-12 13:35:39
【问题描述】:
我正在将旧的普通 PHP RESTful API 项目迁移到 Lumen API 项目中。我被困在 Eloquent 模型的结构上,负责从数据库中获取、插入、更新或删除数据。我想知道Model的结构和映射关系。
这是一些表结构:
stop {id, stop_name, stop_code, stop_status}
stop_detail {detail_id, stop_id, description, created_on, modified_on}
image {image_id, image_path, description, seq_order, is_thumb}
image_stop_mapping {stop_id, image_id, order}
place {place_id, place_title, description, place_status, order}
image_place_mapping {place_id, image_id, order}
现在,当我访问停止模型时,我希望能够在单个模型访问中访问 stop_detail、stop_images、stop_places、place_images。应该是这样的
public function findByStopId($stopId) {
return Stop::where('id', $stopId)->with('stop_detail', 'stop_detail.stop_images', 'places', 'places.images')->get();
}
谁能帮我创建更好的 Eloquent 模型结构?
【问题讨论】:
-
您需要更多地研究模型关系等,例如 hasMany、hasOne、manyToMany 等
-
查看 laravel.com/docs/master/eloquent-relationships 以正确定义 Stop 模型中的关系,然后您将能够使用
with()/load()方法正确加载它们 -
其中一个答案对您有帮助吗?