Many To Many relationship - belongsToMany() - Laravel Eloquent Relationships

Một chú ý đặc biệt về cách đặt key cho belongsToMany

app\Models\Admin.php

app\Models\Role.php

app\Http\Controllers\UserController.php

Ví dụ 1: Create Tag

Ví dụ 2: Tạo post_tag

Ngoài ra chúng ta cũng có thể sử dụng $post->tags()->sync([1,2,3,4]); để update tag

Đặc biệt chúng ta có thể viết ngắn gọn

Ví dụ 3: Get tags of post

Ví dụ 4: Delete tags of post

Ví dụ 5: belongsToMany

C:\xampp\htdocs\hanam.com\app\Tag.php

C:\xampp\htdocs\hanam.com\resources\views\tags\index.blade.php

C:\xampp\htdocs\hanam.com\routes\web.php

Last updated

Was this helpful?