Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Many-To-Many in Laravel

Shulou Source: shulou.com Published: 2022-06-01 02:31:06 09月22日 Update

This article mainly introduces the relevant knowledge of how to use Many-To-Many in Laravel, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use Many-To-Many in Laravel. Let's take a look at it.

In actual development, we are often exposed to several common correspondence patterns:

One-To-One / / one-to-one One-To-Many / / one to many Many-To-Many / / many to many

When I first came into contact with these concepts, I didn't quite understand them. But once you apply these concepts to life, it's easy to understand. Take an example that we often see online:

User-To-Profile / / One-To-OneUser-To-Articles / / One-To-ManyArticles-To-Tags / / Many-To-Many

The translation is:

One user corresponds to one user profile

A user can publish multiple articles

There is indeed a many-to-many relationship between articles and tags. An article can have multiple tags; a tag can belong to multiple articles.

Among these relationship models, the most difficult to implement is the many-to-many relationship like Many-To-Many, but with the help of Laravel's powerful Eloquent, it is relatively smooth to achieve this function.

1. Create a database table

Create an articles table

Schema::create ('articles', function (Blueprint $table) {$table- > increments (' id'); $table- > string ('title'); $table- > text (' content'); $table- > timestamps ();})

Create a tags table

Schema::create ('tags', function (Blueprint $table) {$table- > increments (' id'); $table- > string ('name'); $table- > timestamps ();})

Of course, these two tables alone are not enough to solve this classic problem. You need to create a relational table in addition to these two tables to connect article and tag. In Laravel, if you follow the official standard rules, the third table should look like this:

Table name article_tag

Schema::create ('article_tag', function (Blueprint $table) {$table- > integer (' article_id')-> unsigned ()-> index (); $table- > foreign ('article_id')-> references (' id')-> on ('articles')-> onDelete (' cascade'); $table- > integer ('tag_id')-> unsigned ()-> index () $table- > foreign ('tag_id')-> references (' id')-> on ('tags')-> onDelete (' cascade');})

If you do not follow the official specification, you need to specify a foreign key in the model.

two。 Create a model and specify a relationship

In Article.php:

Public function tags () {return $this- > belongsToMany ('App\ Tag');}

In Tag.php:

Public function articles () {return $this- > belongsToMany ('App\ Article');}

Pay attention to two points here:

You can specify a foreign key when declaring a relationship, such as $this- > belongsToMany ('App\ Article','foreign_key',' other_key')

If you add timestamps () to the article_ tag table, that is, the fields created_at and updated_at appear in the table, you need to declare the relationship in Article like this: return $this- > belongsToMany ('App\ Tag')-> withTimestamps ()

3. Use in Controller

If we want to see which tags an article contains, we can do this:

$article = Article::find ($id); dd ($article- > tags)

If we want to find an article through a tag:

Public function showArticleByTagName ($name) {$tag = Tag::where ('value','=',$name)-> first (); dd ($tag- > articles);} this is the end of the article on "how to use Many-To-Many in Laravel". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Many-To-Many in Laravel". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Tags articles time models users knowledge content official that is concept contact powerful smooth one-on-one two middle finger value example function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft Shulou Technology Apple Redmi