<?php
use Illuminate\Support\Facades\Route;
use App\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']);
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Post;
use App\Models\Video;
use App\Models\Comment;
class TestController extends Controller
{
public function index($id)
{
$post = Post::find($id);
foreach ($post->comments as $comment) {
echo $comment->body . "<br/>";
}
}
public function index2($id)
{
$video = Video::find($id);
foreach ($video->comments as $comment) {
echo '<pre>';
var_export($comment);
echo '<pre>';
}
}
public function index3($id)
{
$video = Video::find($id);
foreach ($video->comments as $comment) {
echo '<pre>';
var_export($comment);
echo '<pre>';
}
}
public function index4($id)
{
$post = Post::find($id);
$comment = new Comment;
$comment->body = "Lorem ipsum dolor sit amet, consectetur adip 1";
$post->comments()->save($comment);
}
public function index5($id)
{
$video = Video::find($id);
$comment = new Comment;
$comment->body = "Lorem ipsum dolor sit amet, consectetur adip 2";
$video->comments()->save($comment);
}
public function savepost($id)
{
$post = Post::find($id);
$comment = new Comment;
$comment->body = "text";
$post->comments()->save($comment);
}
public function savevideo($id)
{
$video = Video::find($id);
$comment = new Comment;
$comment->body = "text";
$video->commcommentsent()->save($comment);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments("id");
$table->string('title');
$table->string('body');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateVideosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('videos', function (Blueprint $table) {
$table->increments("id");
$table->string('title');
$table->string('url');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('videos');
}
}p
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments("id");
$table->string('body');
$table->integer('commentable_id');
$table->string('commentable_type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
C:\xampp\htdocs\songkhoe\app\Models\Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'title',
'body'
];
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Post;
use App\Models\Video;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
User::factory(10)->create();
Post::factory(10)->create();
Video::factory(10)->create();
}
}
Mẫu 1.2 sử dụng model App\Models\Video
C:\xampp\htdocs\songkhoe\app\Models\Video.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'title',
'url'
];
/**
* Get all of the video's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class VideoFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->text(6),
'url' => $this->faker->url()
];
}
}
C:\xampp\htdocs\songkhoe\app\Models\Comment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'body',
'commentable_id',
'commentable_type'
];
/**
* Get the parent commentable model (post or video).
*/
public function commentable()
{
return $this->morphTo();
}
}
In this tutorial, one to many polymorphic laravel examples, I will explain what is polymorphic relationship and when we can use it in our laravel application. One to Many Polymorphic Model Relationships is used when a model belongs to more than one other model on a single association model. For example, If we have posts and videos tables, both need to add a comments system.
A one-to-many polymorphic relationship in laravel is similar to a one-to-many relation. However, if the child model can belong to more than one type of model using a single association then we can use this one-to-many polymorphic relationship in laravel.
In this laravel one to many polymorphic relationship tutorial, we can understand how to create migration with a foreign key schema for polymorphic one to many eloquent relationship, use sync with a pivot table, create records, get all data, delete, update and everything related to one to many polymorphic relationship.
One To Many (Polymorphic) Scenario
Taking the example mentioned above into consideration, we have two entities: Post and Video. To allow for comments on each of these, we can decide to set up our database like this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
PHPCopy
Model Structure of Polymorphic Relationship
Now let's define the one-to-many polymorphic relationship in our model class.
app/Models/Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
PHPCopy
Now define the relationship in the Video model like:
app/Models/Video.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
/**
* Get all of the video's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
PHPCopy
Now define the relationship in the Comment model like:
app/Models/Comment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get the parent commentable model (post or video).
*/
public function commentable()
{
return $this->morphTo();
}
}
Once our database table and models are defined, we may access the relationships via your model's dynamic relationship properties. To access the comments for a Post, we can use the image a property declared in the model.
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$post = Post::find($id);
foreach ($post->comments as $comment) {
//
}
}
}
PHPCopy
For retrieving comments for a video:
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\Video;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$video = Video::find($id);
foreach ($video->comments as $comment) {
//
}
}
}
PHPCopy
Save Polymorphic Relationship Data
If we want to save or create polymorphic relationship data then we can follow the below structure:
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$post = Post::find($id);
$comment = new Comment;
$comment->body = "text";
$post->comment()->save($comments);
}
}
We have tried to discuss the basic concept of one to many polymorphic relationships and their possible use cases in laravel applications. We should also note that one 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.