douti9286 2017-02-10 11:03
浏览 69
已采纳

Laravel与多个模型的关系

I am building a blog application and I have

Post, City, Neighbourhood, SubNeighbourhood.

The relations that I have until now are between City, Neighbourhood and subNeighbourhood like follows:

subNeighbourhood belongs to Neighbourhood 
Neighbourhood belongs to city

The second relation is like

City hasMany Neighbourhoods 
Neighbourhood hasMany subNeighbourhoods 

My problems are when I create the post. The posts table have id_city, id_neighbourhood and id_subNeighbourhood.

The questions that I have are:

If the post structure is correct? I feel like having many "id_..." is not ok.

Second issue is how do I write the relation in posts? All the columns "id_..." are having foreign keys to each corresponding table.

  • 写回答

2条回答 默认 最新

  • doujia2463 2017-02-10 11:18
    关注

    For what I can understand from your explanation the post table should have only a subneighbourhood_id field, in the Post class the relation should be:

    public function subneighbourhood()
    {
        return $this->belongsTo('App\Subneighbourhood');
    

    So a Post belongs to a Subneighbourhood that belongs to a Neighbourhood that belongs to a City

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作