X
    Categories: Laravel

Laravel polymorphic relationships example

Laravel polymorphic relationships example

Hi, In this tutorial we are going to see how laravel is dealing with polymorphic(morphto) relationships.

Lets Assuse we have 3 tables :

1. Prodcuts => Collections of Products
2. Photos => Collections of Images
3. Notes => Common Table for all other tables.

Creating Tables :

1. Prodcuts

php artisan make:migration create_products_table
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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

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

2. Photos

php artisan make:migration create_photos_table
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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

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

3. Notes

php artisan make:migration create_notes_table
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNotesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('notes', function(Blueprint $table){
            $table->increments('id');
            $table->integer('noteable_id')->unsigned()->index();
            $table->string('noteable_type')->nullable();
            $table->string('notes')->nullable();
            $table->timestamps();
        });
    }

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

Create Models :

* Note : App\\Models is my Models path

 

php artisan make:model App\\Models\\Product
php artisan make:model App\\Models\\Photo
php artisan make:model App\\Models\\Note

App\\Models\\Product

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    /**
     * Get all of the post's notes.
     */
    public function notes()
    {
        return $this->morphMany('App\Models\Note', 'noteable');
    }
}

App\\Models\\Photo

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    /**
     * Get all of the post's notes.
     */
    public function notes()
    {
        return $this->morphMany('App\Models\Note', 'noteable');
    }
}

App\\Models\\Note

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
	
	protected $fillable = array('noteable_id', 'noteable_type', 'notes');

    /**
     * Get all of the owning noteable models.
     */
    public function noteable()
    {
        return $this->morphTo();
    }
}

Inserting a Records to table :

Insert Product & Note

// Store Product
$product = new Post;
$product->name = 'Laravel Book';
$product->description = 'Clear Explanation about laravel routing';
$product->save();

// Store Notes for Product
$note = new Note(['notes' => 'I love this product']);
$product->notes()->save($note);

Insert Photo & Note

// Store Photo
$photo = new Photo;
$photo->path = '/image.png';
$photo->save();

// Store Notes for Photo
$note = new Note(['notes' => 'Perfect Click']);
$photo->notes()->save($note);

Accessting Notes :

Access notes for Products :

$product = Product::find(1);
echo $product->notes;

Access notes for Photos :

$photo = Photo::find(1);
echo $photo->notes;

Thats all, Now you have learned about Laravel polymorphic relationship.

Thanks for reading, If you like this article don’t forget to share and comment.
Happy Coding!.

Marimuthu :