laravel morph through code example

Example 1: morph relation laravel

You may register the morphMap in the boot function of your 
App\Providers\AppServiceProvider class or create a separate service provider 
if you wish.

use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'post' => 'App\Models\Post',
    'video' => 'App\Models\Video',
]);

Example 2: laavel relation through morph

// Topic Model

public function users()
{
    return $this->hasManyThrough('App\User', 'App\Subscriber', 'subscribable_id')->where('subscribable_type', array_search(static::class, Relation::morphMap()) ?: static::class);
}

Tags:

Php Example