Last active
August 17, 2022 10:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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