One To Many Polymorphic Relationships In Laravel
Lets start.
How to define One To Many Polymorphic Eloquent Relationship
1.Create laravel project and connect to database in .env file.
composer create-project laravel/laravel VideoApp --prefer-dist
2.Create Model and Migrate it.
To implement One To Many Polymorphic Relationships in Laravel, we need models: Posts,Video
and Comment
.
Now we need to create Posts,Video
and Comment
models in fresh project so for that run below commands in comand prompt.
php artisan make:model Post -m
php artisan make:model Video -m
php artisan make:model Comment -m
3.After Created Models we need to Implement code in Model and migration files as below
2020_09_07_111619_create_posts_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
2020_09_07_132636_create_videos_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVideosTable extends Migration
{
public function up()
{
Schema::create('videos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('url');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('videos');
}
}
2020_09_07_132747_create_comments_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('body');
$table->integer('commentable_id');
$table->string('commentable_type');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('comments');
}
}
Migrate the schema files using the following command
php artisan migrate
Models
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'name','body '
];
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Videos.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
protected $fillable = [
'title','url'
];
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Comments.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = [
'body','commentable_id','commentable_type'
];
public function commentable()
{
return $this->morphTo();
}
}
4.Create route and controller to access data.
Route in Web.php
Route::get('onetomanypoly','PolyController@onetomanypoly');
Create PolyController.php using below command in command prompt.
php artisan make:controller PolyController
Implement code in PolyController.php as below
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\Video;
use App\Post;
class PolyController extends Controller
{
public function onetomanypoly()
{
$post = App\Post::find(1);
foreach ($post->comments as $comment) {
print_r($comment);
}
}
}
You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo
. In our case, that is the commentable
method on the Comment
model. So, we will access that method as a dynamic property:
$comment = App\Comment::find(1);
$commentable = $comment->commentable;
The commentable
relation on the Comment
model will return either a Post
or Video
instance, depending on which type of model owns the comment