duanpu1064 2014-11-17 19:22
浏览 13

在这些表上使用雄辩的常规方法

Having the following tables. What would be the best or conventional way to add relationships to these tables with eloquent?

I know that a Many to Many relationship will need a pivot table. The thing I'm not seeing samples of online is how to do a relationship from one table to multiple tables. In this case being the profiles table to sizes and breeds tables.

dogs
  - id
  - name
  - profile_id

profiles
  - id
  - dog_id
  - size_id
  - breed_id

sizes
  - id
  - type

breeds
  - id
  - name

In the end I'd like to be able to query something like this:

Dogs that are sizes in(x-small, small, medium) and breeds in(chihuahua, maltese).

I don't want to confuse myself or others by posting what I'm guessing for my eloquent code as I'm not even close.

  • 写回答

1条回答 默认 最新

  • duanmei1894 2014-11-18 00:47
    关注

    I think you need to create a database structure like below:

    dogs
      - id
      - name
    
    profiles
      - id
      - size_id
      - breed_id
    
    dog_profile
      - dog_id
      - profile_id
    
    sizes
      - id
      - type
    
    breeds
      - id
      - name
    

    Then, create a model for each table and insert relationships such as described in laravel's documentation. You will need hasMany-belogsTo and belogsToMany-belogsToMany relationships.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!