<?phpuseIlluminate\Support\Facades\Route;useApp\Http\Controllers\TestController;/*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/Route::get('/test/{id}', [TestController::class,'index']);Route::get('/test2/{id}', [TestController::class,'index2']);Route::get('/test3/{id}', [TestController::class,'index3']);Route::get('/test4/{id}', [TestController::class,'index4']);Route::get('/test5/{id}', [TestController::class,'index5']);
<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Factories\HasFactory;useIlluminate\Database\Eloquent\Model;useApp\Models\Post;useApp\Models\Video;classTagextendsModel{useHasFactory;public $timestamps =false;protected $fillable = ['name' ];/** * Get all of the posts that are assigned this tag. */publicfunctionposts() {return$this->morphedByMany(Post::class,'taggable'); }/** * Get all of the videos that are assigned this tag. */publicfunctionvideos() {return$this->morphedByMany(Video::class,'taggable'); }}
<?phpuseIlluminate\Database\Migrations\Migration;useIlluminate\Database\Schema\Blueprint;useIlluminate\Support\Facades\Schema;classCreatePostsTableextendsMigration{/** * Run the migrations. * * @returnvoid */publicfunctionup() {Schema::create('posts',function (Blueprint $table) { $table->increments("id"); $table->string('name'); }); }/** * Reverse the migrations. * * @returnvoid */publicfunctiondown() {Schema::dropIfExists('posts'); }}p
Mẫu 1.3 với nhiều model App\Models\Post
Laravel many to many polymorphic relationship is a bit different from one to many polymorphic relationship. Many-to-many polymorphic relations are a bit more complicated than "morph one" and "morph many" relationships in laravel. For example, a Post model and Video model has a polymorphic relation to a Tag model.
In this tutorial, I will show you how to create polymorphic many-to-many relationships with migration with a foreign key schema for many 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 polymorphic relationship.
In this tutorial, I will create "posts", "videos", "tags" and "taggables" tables to show you an example. Each table will be connected with each other. Now we will create many to many polymorphic relationships with each other by using laravel Eloquent Model.
Many To Many (Polymorphic) Scenario
First, let's examine the table structure required to build this many to many polymorphic relationships:
posts id -integer name -stringvideos id -integer name -stringtags id -integer name -stringtaggables tag_id -integer taggable_id -integer taggable_type -string
PHPCopy
Model Structure of Polymorphic Relationship
Now let's define the many-to-many polymorphic relationship in our model class.
app/Models/Post.php
namespaceApp\Models;useIlluminate\Database\Eloquent\Model;classPostextendsModel{/** * Get all of the tags for the post. */publicfunctiontags() {return$this->morphToMany(Tag::class,'taggable'); }}
PHPCopy
Now define the relationship in the Video model like:
app/Models/Video.php
namespaceApp\Models;useIlluminate\Database\Eloquent\Model;classVideoextendsModel{/** * Get all of the tags for the post. */publicfunctiontags() {return$this->morphToMany(Tag::class,'taggable'); }}
PHPCopy
Now define the relationship in the Tag model like:
app/Models/Tag.php
<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Model;classTagextendsModel{/** * Get all of the posts that are assigned this tag. */publicfunctionposts() {return$this->morphedByMany(Post::class,'taggable'); }/** * Get all of the videos that are assigned this tag. */publicfunctionvideos() {return$this->morphedByMany(Video::class,'taggable'); }}
PHPCopy
Retrieving The Relationship
Once our database table and models are defined, we may access the relationships via your model's dynamic relationship properties. For example, to access all of the tags for a post, you can use the tags dynamic relationship property:
App\Http\Controllers\TestController.php
<?phpnamespaceApp\Http\Controllers;useApp\Models\Post;useIlluminate\Http\Request;classTestControllerextendsController{publicfunctionindex($id) { $post =Post::find($id);foreach ($post->tags as $tag) {// } }}
PHPCopy
For retrieving tags for a video:
App\Http\Controllers\TestController.php
<?phpnamespaceApp\Http\Controllers;useApp\Models\Video;useIlluminate\Http\Request;classTestControllerextendsController{publicfunctionindex($id) { $video =Video::find($id);foreach ($video->tags as $tag) {// } }}
PHPCopy
You can also retrieve the parent of a polymorphic relation from the polymorphic child model that performs the call to morphedByMany. In this case, that is the posts or videos methods on the Tag model:
<?phpuseApp\Models\Tag;$tag =Tag::find(1);foreach ($tag->posts as $post) {//}foreach ($tag->videos as $video) {//}
I have tried to discuss the clear concept of many to many polymorphic relationships and their possible use cases in laravel applications. We should also remember that many to many polymorphic relationships are not a perfect solution to everything and should only be used when convenient or feels like the right way to go.