Skip to content

Instantly share code, notes, and snippets.

@jangaraev
Last active August 17, 2022 10:13
Show Gist options
  • Save jangaraev/3ea21aee8671d229c0219caa77392250 to your computer and use it in GitHub Desktop.
Save jangaraev/3ea21aee8671d229c0219caa77392250 to your computer and use it in GitHub Desktop.
Specifying map for morph relationships which enables short/human-friendly model names in DB
<?php
namespace App\Providers;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class MorphMapServiceProvider extends ServiceProvider
{
public function boot()
{
// limit a length of varchar columns
// Schema::defaultStringLength(191);
// don't forget to register the service provider in config/app.php
// it can also be put to AppServiceProvider
Relation::morphMap([
'foo' => \App\Models\Foo::class,
'bar' => \App\Models\Bar::class,
// ...
'cat' => \App\Models\Cat::class,
'dog' => \App\Models\Dog::class
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment