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?
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.