Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand Laravle eloquent many-to-many model association

Shulou Source: shulou.com Published: 2022-06-03 09:08:35 09月30日 Update

This article focuses on "how to understand Laravle eloquent many-to-many model association", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Laravle eloquent many-to-many model association.

What is a many-to-many relationship?

A model in Eloquent is a data table, which is usually associated with each other. Many-to-many association means that two tables have many relationships with each other. For example, a table stores user data.

Another table stores information about the article.

A user can collect multiple articles, and an article can also be collected by multiple users, which is a many-to-many association.

How to use many-to-many associations?

Many-to-many associations using Eloquent can easily query, modify, add, and delete associations between the two models.

A many-to-many association requires a record associated table (pivot table) in addition to the two related tables, which generally records the ID of the two models

Take a chestnut.

The three tables in our database are user.

Articles

Article_collections

We need to create two models, and the intermediate table model is not required.

Tags: Associations models methods data parameters that is users between queries two content datasheets articles articles learning convenience practicality deeper three information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Xiaomi vpn NVidia