dongshadu4498 2014-10-22 15:02
浏览 44

Laravel模型关系。 纯DB相关

I was reading Laravel docs about defining relationships and it all was a bit unclear to me. (could because of language barrier.. )

I got the following:

I want people to choose an objective. If user chooses a objective, the related packages show up. In these packages there is a info button, containing further info about the package. If user clicks on the info link, the related package info will show up.

FYI! There will be 9 objectives. Each objective has 3 packages and each package has 1 packageinfo.

Objective Model
// contains
- id
- name
- icon

// relationship
HasMany package

////////////////////////////////////////////////////
Package Model
// contains
- id
- title
- information
- info_link
- buy_link

// relationship
belongsToMany Model
hasMany PackageInfo -- or has PackageInfo ?

///////////////////////////////////////////////////
PackageInfo Model
// contains
- id
- body

// relationship
belongsToMany Package -- or belongsTo Package ?


//////////////////////////////////////////////////
Objective_package Pivot
- id
- Objective_id
- Package_id

package_packageInfo pivot
- id
- Package_id
- packageinfo_id

Am i defining the relationships correct or did i really read the whole doc like upside-down?

  • 写回答

1条回答 默认 最新

  • douzhi3586 2014-10-22 15:35
    关注

    The objective to package relationship seems correct but if there is only 1 packageinfo then you do not need a pivot table for that relationship

    //PackageInfo Model
    - id
    - package_id
    - body
    
    //relationship
    belongsTo Package
    $this->belongsTo('Package');
    

    The quickest answer I can give to help explain this is hasOne would be for data that belongs to the parent object but may not always be required to be pulled with it. As in a User has a Profile. 90% if not more of the time you do not want their bio or profile but that profile is exclusive to that User.

    hasMany would be like Addresses. A user could have multiple addresses either for shipping or billing but no other user should have access to that data so this one User hasMany Address

    belongsToMany, or Many To Many, is shared between multiple objects. a User can have many Roles and a Role will have many Users. A blog's Post could have many Tags and a Tag would be attached to many Posts.

    Polymorphic; you may never really need to worry about but the idea is an Image could belong to many different objects but in different ways. As in a member of a Gallery, or a User's thumbnail or a Blog title image, etc. This breaks relational data mapping in the name of code and reusability. Discretion is advised as many have different opinions on how/when to use this.

    Hope this helps. If there is anything I an attempt to help you with or if I missed what you were looking for, please let me know.

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题