belongsTorelationship is a
hasOnerelationship. Taking the same example as before, the opposite of "an address
belongsToa customer" is simply "a customer
belongsTorelationship. Even though we recommend you modify your database structure to stay within foreign key conventions (pointing to an id), there is a way to specify how your tables are linked.
fk_customernameof a table Address points to the field
nameof a table Customer, add the following:
belongsToManyrelationship (SQL only)
belongsToManyassociation is often used to set up a many-to-many relationship with another model. For this example, we will consider the models
Users. A user can be part of many projects, and one project has many users. The junction table that will keep track of the associations will be called
userProjects, which will contain the foreign keys projectId and userId.
belongsTois straight forward, we check if the referenced table of the foreign key is unique (unique constraint or primary key), then a
belongsToassociation can be set between the two tables.
belongsTorelationship to the corresponding collection.
hasManyrelationship to the corresponding collection.