😎Laravel 9 Many to Many Eloquent Relationship Tutorial, full (ok)
https://www.itsolutionstuff.com/post/laravel-9-many-to-many-eloquent-relationship-tutorialexample.html
Một chú ý rất quan trọng sử dụng belongsTo

Trường hợp 1 sử dụng product_id làm khóa phụ



Trường hợp 2 sử dụng order_id làm khóa phụ


Dùng find sao một số trường hợp không ra kết quả?
C:\xampp\htdocs\hanam\resources\views\admin\show_order_detail.blade.php

Ví dụ 1
Source Code:
Bên này sử dụng cho các ví dụ One to One, One to Many
C:\xampp\htdocs\test\routes\web.php
Ví dụ 1: cho mối quan hệ 1-1 C:\xampp\htdocs\test\app\Http\Controllers\UserController.php


C:\xampp\htdocs\test\app\Http\Controllers\PostController.php






C:\xampp\htdocs\test\app\Models\Comment.php
C:\xampp\htdocs\test\app\Models\Phone.php
C:\xampp\htdocs\test\app\Models\Post.php
C:\xampp\htdocs\test\app\Models\Role.php
C:\xampp\htdocs\test\app\Models\User.php
C:\xampp\htdocs\test\app\Models\UserRole.php
C:\xampp\htdocs\test\database\factories\CommentFactory.php
C:\xampp\htdocs\test\database\factories\PhoneFactory.php
C:\xampp\htdocs\test\database\factories\PostFactory.php
C:\xampp\htdocs\test\database\factories\RoleFactory.php
C:\xampp\htdocs\test\database\factories\UserFactory.php
C:\xampp\htdocs\test\database\factories\UserRoleFactory.php
C:\xampp\htdocs\test\database\migrations\2014_10_12_000000_create_users_table.php
C:\xampp\htdocs\test\database\migrations\2014_10_12_100000_create_password_resets_table.php
C:\xampp\htdocs\test\database\migrations\2019_08_19_000000_create_failed_jobs_table.php
C:\xampp\htdocs\test\database\migrations\2019_12_14_000001_create_personal_access_tokens_table.php
C:\xampp\htdocs\test\database\migrations\2022_05_05_171115_create_phones_table.php
C:\xampp\htdocs\test\database\migrations\2022_05_05_180005_create_posts_table.php
C:\xampp\htdocs\test\database\migrations\2022_05_05_180155_create_comments_table.php
C:\xampp\htdocs\test\database\migrations\2022_05_06_020336_create_roles_table.php
C:\xampp\htdocs\test\database\migrations\2022_05_06_020431_create_role_user_table.php
C:\xampp\htdocs\test\database\seeders\CommentSeeder.php
C:\xampp\htdocs\test\database\seeders\PhoneSeeder.php
C:\xampp\htdocs\test\database\seeders\PostSeeder.php
C:\xampp\htdocs\test\database\seeders\RoleSeeder.php
C:\xampp\htdocs\test\database\seeders\UserRoleSeeder.php
C:\xampp\htdocs\test\database\seeders\UserSeeder.php
Ví dụ 2.1: Many to Many
app\Http\Controllers\UserController.php
app\Models\Role.php
app\Models\User.php
database\migrations\2022_08_24_112636_create_table_role_user_table.php
database\migrations\2022_08_24_112543_create_table_roles_table.php
database\seeders\RoleSeeder.php
database\factories\RoleFactory.php




Ví dụ 2.2: Many to Many


Ví dụ 2.3: Many To Many

app\Http\Controllers\UserController.php
Ví dụ 2.4.1: Many To Many lấy giá trị trung gian (Theo mặc định, chỉ có các keys sẽ có mặt trên các pivot object.) đọc thêm

Kết quả:

Nếu bạn muốn bảng pivot của bạn tự động có created_at và updated_at timestamps, sử dụng các phương thức Timestamps vào trong định nghĩa của mối quan hệ:
C:\xampp8\htdocs\testauth\app\Models\User.php
C:\xampp8\htdocs\testauth\database\migrations\2023_01_04_101502_create_roles_table.php
C:\xampp8\htdocs\testauth\database\migrations\2023_01_04_101537_create_role_user_table.php
C:\xampp8\htdocs\testauth\database\factories\RoleFactory.php
C:\xampp8\htdocs\testauth\database\factories\RoleUserFactory.php
C:\xampp8\htdocs\testauth\database\seeders\DatabaseSeeder.php
C:\xampp8\htdocs\testauth\app\Http\Controllers\UserController.php
C:\xampp8\htdocs\testauth\routes\web.php
Ví dụ 2.4.2: Many To Many lọc giá trị trong bảng trung gian (Đọc thêm)

C:\xampp8\htdocs\testauth\app\Models\User.php
Kết quả:

Laravel 9 Many to Many Eloquent Relationship Tutorial
By Hardik Savani March 28, 2022 Category : LaravelPauseUnmuteLoaded: 2.69%Fullscreen
Hi,
Today our leading topic is many to many relationship laravel 9. I would like to share with you laravel 9 many to many relationship example. This tutorial will give you a simple example of laravel 9 belongstomany tutorial. This tutorial will give you simple example of laravel 9 many to many sync.
So in this tutorial, you can understand how to create many-to-many relationships with migration with a foreign key schema for one to many relationships, use sync with a pivot table, create records, attach records, get all records, delete, update, where condition and everything related to many to many relationship.
In this example, i will create "users", "roles" and "role_user" tables. each table is connected with each other. now we will create many to many relationships with each other by using the laravel Eloquent Model. We will first create database migration, then model, retrieve records and then how to create records too. So you can also see the database table structure on the below screen.

Many to Many Relationship will use "belongsToMany()" for relation.
Create Migrations:
Now we have to create migration of "users", "roles" and "role_user" table. we will also add foreign key with users and roles table. so let's create like as below:
users table migration:
roles table migration:
role_user table migration:
Create Models:
Here, we will create User, Role and UserRole table model. we will also use "belongsToMany()" for relationship of both model.
User Model:
Role Model:
UserRole Model:
Retrieve Records:
Create Records:
Read Also: Laravel 9 One to Many Eloquent Relationship Tutorial
I hope you understand of many to many relationship...
Last updated
Was this helpful?
