Hello Devs,

In this tutorial, we are going to learn how to use One to Many polymorphic eloquent relationship in laravel application.

Follow this step by step guide given below:




Step:1 

Create Migration

php artisan make:model Project -m

php artisan make:model Video -m

php artisan make:model Message -m


Path:database\migrations\2014_10_12_000000_create_project_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProjectTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('project', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('project');
    }
}

Path:database\migrations\2014_10_12_000000_create_videos_table.php

<?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->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('videos');
    }
}

Path:database\migrations\2014_10_12_000000_create_messages_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMessagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->text('body');
            $table->integer('message_id');
            $table->string('message_type');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('messages');
    }
}

Run this command:

php artisan migrate



Step:2 

Create Model Relationship

app\Project.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    protected $fillable = ['name'];
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function message()
    {
        return $this->morphMany(Message::class, 'message');
    }
}

app\Video.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    protected $fillable = ['name'];
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function message()
    {
        return $this->morphMany(Message::class, 'message');
    }
}

app\Message.php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Message extends Model
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    protected $fillable = ['body','message_id','message_type'];
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function message()
    {
        return $this->morphTo();
    }
}



Step 3 : 

Insert Records

app\Http\Controllers\ProjectController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Project;
use App\Video;
use App\Message;

class ProjectController extends Controller
{
    public function project()
    {   
       //project message add
       $project = Project::find(1);   
 
       $message = new Message;
       $message->body = "Hi nicesnippets.com";
     
       $project->messages()->save($message);

       //video message add
       $video = Video::find(1);   
 
       $message = new Message;
       $message->body = "Hi nicesnippets.com";
     
       $video->messages()->save($message);
    }
}



Step 4 : 

Retrieve Records

app\Http\Controllers\ProjectController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Project;
use App\Video;
use App\Message;

class ProjectController extends Controller
{
    public function project()
    {   
       //project message retrieve 
       $project = Project::find(1);   
       dd($project->messsage);

       //video message Retrieve
       $video = Video::find(1);   
       dd($video->message);
     }
}


I hope this example helps you.