<?phpuseIlluminate\Support\Facades\Route;useApp\Http\Controllers\TestControlleras 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('/',function () {returnview('welcome');});Route::get('test/{id}', [TestController::class,'index'])->name('index');Route::get('test2/{id}', [TestController::class,'index2'])->name('index2');Route::get('test3/{id}', [TestController::class,'index3'])->name('index3');
<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Factories\HasFactory;useIlluminate\Database\Eloquent\Model;classPostextendsModel{useHasFactory;/** * The attributes that are mass assignable. * * @vararray<int, string> */public $timestamps =false;protected $fillable = ['name' ];/** * Get the post's image. */publicfunctionimage() {return$this->morphOne(Image::class,'imageable'); }}
C:\xampp\htdocs\wayarmy\app\Models\Image.php
<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Factories\HasFactory;useIlluminate\Database\Eloquent\Model;classImageextendsModel{useHasFactory;public $timestamps =false;protected $fillable = ['url','imageable_id','imageable_type' ];/** * Get the parent imageable model (user or post). */publicfunctionimageable() {return$this->morphTo(); }}
<?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'); }}
Mẫu 1.2 xây dựng type model App\Models\User
C:\xampp\htdocs\wayarmy\routes\web.php
<?phpuseIlluminate\Support\Facades\Route;useApp\Http\Controllers\TestControlleras 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('/',function () {returnview('welcome');});Route::get('test/{id}', [TestController::class,'index'])->name('index');Route::get('test2/{id}', [TestController::class,'index2'])->name('index2');Route::get('test3/{id}', [TestController::class,'index3'])->name('index3');Route::get('test4/{id}', [TestController::class,'index4'])->name('index4');
In this laravel 9 polymorphic relationship example, I will discuss about polymorphic relationship in laravel. In this example, I will discuss about laravel polymorphic one to one example. I will discuss about what is polymorphic relationship is and when we need this relationship in the Laravel application.
In this example, we will see polymorphic relationship laravel, laravel polymorphic one to one example, laravel polymorphic migration and laravel save polymorphic relationship. You need to just follow this tutorial to learn the above scenarios.
Polymorphic Relationships in Laravel
In Laravel, a polymorphic relationship allows the child model to belong to more than one type of model using a single association. For example, imagine you are building an application that allows users to share their feedback for posts and videos. In such an application, a Comment model might belong to both the Blog and Video models. Now we know what is polymorphic relationship in laravel. Now we will see when we will use it in our Laravel application.
One To One (Polymorphic) Scenario
Taking the example mentioned above into consideration, we have two entities: Post and User. To allow for images on each of these, we can decide to set up our database like this:
posts id - integer name - stringusers id - integer name - stringimages id - integer url - string imageable_id - integer imageable_type - string
PerlCopy
If we avoid polymorphic relationship schema, then our database schema will look like:
posts: id title contentposts_images: id post_id image dateusers: id nameusers_images: id user_id image date
PHPCopy
But look at that our polymorphic schema, how cool it is. Here, we have three entities: Post, User, and Image. Post can have Image. User can have Image. Let's create our migration like:
Remember: $table→morphs('commentable') would automatically create two columns for the id and type using the text passed to it, so it will result in commentable_id and commentable_type.
Define One to One Polymorphic Relationship
Now let's define the one to one polymorphic relationship in our model class.
app/Models/User.php
<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Model;classUserextendsModel{/** * Get the user's image. */publicfunctionimage() {return$this->morphOne(Image::class,'imageable'); }}
PHPCopy
Now define the relationship in the Post model like:
app/Models/Post.php
<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Model;classPostextendsModel{/** * Get the post's image. */publicfunctionimage() {return$this->morphOne(Image::class,'imageable'); }}
PHPCopy
Now define the relationship in the Image model like:
app/Models/Image.php
<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Model;classImageextendsModel{/** * Get the parent imageable model (user or post). */publicfunctionimageable() {return$this->morphTo(); }}
PHPCopy
To access the image for a User, we can use the image property declared in the model.