Laravel One to Many Polymorphic Eloquent Relationship Example

10-Apr-2023

.

Admin

Laravel One to Many Polymorphic Eloquent Relationship Example

Hi Guys,

In this tutorial, I Will explain you how to use One to Many polymorphic eloquent relationship in laravel application. We can create migration with foreign key schema, retrieve records, insert new records, records etc. I will show you laravel morphMany relationship example.

We will create three table "projects", "videos" and "messages" and both table are connected with each other, I will use One to Many polymorphic relationship with each other by using laravel Eloquent Model, We will first create database migration, then model, retrieve records and then how to create records too.One to Many polymorphic Relationship use "morphMany()" and "morphTo()" for relation.

Here, I will give you full example for laravel One to Many polymorphic eloquent relationship as bellow.

Step:1 Create Migration


In this example, we will need two tables in our database project, videos and messages.I will start to create the model and migrations, then we will define the one to morphMany relationship.To generate models and migrations, run below two commands in your terminal.

php artisan make:model Project -m

php artisan make:model Video -m

php artisan make:model Message -m

Above command will generate two models in your app folder called project, videos and messages. You will also find two new migrations in your database/migrations folder.

Your newly created models and migration after add table field:

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');

}

}

Now run the below command to create project, videos and messages tables in your database:

php artisan migrate

Step:2 Create Model Relationship

Now in this step, we have project, videos and messages tables ready, let’s define the one to manMany relationship in our models.

our app\project.php model file and add a new function called message() which will return a morphMany relationship like below:

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');

}

}

you can see, in the message() method we are defining a morphMany relationship on message model.

Now we will defining Inverse One to Many polymorphic Relationship in Laravel.As we have defined the morphMany relationship on Brand model which return the related records of message model, we can define the inverse relationship on the message model.Open your app\message.php model file and add a new method called message() in it which will return the related brand of a 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();

}

}

As you can see, in message() method we are returning a belongs to relation which will return the related project of the message.

Step 3 : Insert Records

Let’s in this step add data to project, videos and messages from controller:

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

Now in this step retrieve records model

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);

}

}

it will help you....

#Laravel 7

#Laravel

#Laravel 6