Model Relationships

Below the properties, we have a couple functions.

The first function describes a relationship between a note and a user.

If a table has a field that has _id, the first part refers to another database table. In this case, it’s referring to the user table. Since the notes table has this field, it means that each note model “belongs to” a user model. The user model has a corresponding function call “hasMany.” It’s a “has many” relationship because the user table does not have a note_id field. Just remember. “Belongs to” means that the model/table should have a field with _id. The table it belongs to is the other part of the field (user).

Adding this function to the model means we can call the function (user, in this case) to get the user that has this note. If we had a note, we would call $note->user to get the user. Remember. The user does not belong to the note. It doesn’t have a note_id field. The user has many notes.

Thet’s all we need to look at for now.