Polymorphic relationships in Laravel 5.7 Example

Today, We want to share with you Polymorphic relationships in Laravel 5.7 Example.In this post we will show you laravel polymorphic table migration, hear for Laravel Polymorphic Relationship Tutorial we will give you demo and example for implement.In this post, we will learn about laravel query polymorphic relationship with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Polymorphic relationships in Laravel 5.7 Example

There are the Following The simple About Polymorphic relationships in Laravel 5.7 Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel hasmanythrough pivot, so the some laravel eloquent relationships for this example is following below.

Laravel Eloquent Relationships Tutorial with Examples

Laravel Many to Many Polymorphic Relationship Tutorial

class CreatecollageTable extends Migration
{
    public function up()
    {
        Schema::create('collage', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('Collageable_id');
            $table->string('Collageable_type');
            $table->string('name');
            $table->timestamps();
        });
    }
}

With the model relationships defined like so.

class Collage extends Model
{
    public function Collageable()
    {
        return $this->morphTo();
    }
}

class Student extends Model
{
    public function collage()
    {
        return $this->morphMany(Collage::class, 'Collageable');
    }
}

class Teacher extends Model
{
    public function collage()
    {
        return $this->morphMany(Collage::class, 'Collageable');
    }
}

Good Luck! That’s all that is needed for this Laravel 5.7 relation. Making use of it is no different from defining a standard Simple Polymorphic one-to-many relationship.

// assign a Collage to a Student
$Student->collage()->create(['name' => 'Increase conversion rate']);

// get all collage associated with an Teacher
$collage = $Teacher->collage;

Simple Laravel 5.7 Polymorphic relationships Examples

class CreateleavesTable extends Migration
{
    public function up()
    {
        Schema::create('leaves', function (Blueprint $table) {
            $table->unsignedInteger('Collage_id');
            $table->foreign('Collage_id')->references('id')->on('collage');
            $table->unsignedInteger('Leaveable_id');
            $table->string('Leaveable_type');
            $table->string('value');
            $table->enum('type', ['target', 'start', 'running', 'end']);
            $table->timestamps();
        });
    }
}
[/php

<p>With the Laravel 5.7 model relationships defined like so.</p>
[php]
class Leave extends Model
{
    protected $guarded = [];

    public function Leaveable()
    {
        return $this->morphTo();
    }
}

class Collage extends Model
{
    public function customDatas()
    {
        return $this->morphedByMany(Student::class, 'Leaveable', 'leaves')
            ->withTimestamps()
            ->withPivot(['value', 'type']);
    }

    public function customDatas()
    {
        return $this->morphedByMany(customData::class, 'Leaveable', 'leaves')
            ->withTimestamps()
            ->withPivot(['value', 'type']);
    }
}

class Student extends Model
{
    public function leaves()
    {
        return $this->morphToMany(Collage::class, 'Leaveable');
    }
}

class customData extends Model
{
    public function leaves()
    {
        return $this->morphToMany(Collage::class, 'Leaveable');
    }
}

Interacting with these Laravel 5.7 relationships is still extremely simple

// get all collage for a customData
$leaves = $customData->leaves;

// output all Leave values for all collage
@foreach($leaves as $Leave)
      {{ $Leave->pivot->value }}
@endforeach

// store an Leave for a Student
$Collage = $Student->collage()->create(
    ['name' => 'Increase conversion your rate'
]);

$Collage->leaves()->attach($Student, [
    'type' => 'target', 
    'value' => '9898'
]);

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Polymorphic relationships in Laravel 5.7 Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel Grouping Cache Offset & Limit Eloquent methods
  2.   how to create One to Many Relationship in laravel?
  3.   Laravel dynamic breadcrumbs with links
  4.   PHP Webcam Capture Image and Save from Camera
  5.   Eloquent orwhere Query Use in Laravel
  6.   Laravel 6 Inserting & Updating Eloquent Models
  7.   Vue IF Else Conditional Statement
  8.   AngularJS Insert using PHP MySQLi
  9.   Laravel 6 MySQL Get Last Inserted ID
  10.   Laravel 6 group by month/year using Eloquent

Leave a Reply

Your email address will not be published. Required fields are marked *