Skip to content

Latest commit

 

History

History
46 lines (39 loc) · 937 Bytes

eloquent-morphmap-autoloader.md

File metadata and controls

46 lines (39 loc) · 937 Bytes

MorphMap AutoLoader

<?php
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

$namespace = 'App\\Models';

$morphMap = Collection::make(get_declared_classes())
   ->filter(function($class) use ($namespace) {
       return (
           Str::startsWith($class, $namespace) &&
           is_subclass_of($class, Model::class)
       );
   })
   ->mapWithKeys(function($class){
       $mapName = Str::snake(Str::plural(class_basename($class)));
       return [$mapName => $class];
   })
   ->toArray();

Relation::morphMap($morphMap);

dd($morphMap);

use Illuminate\Database\Eloquent\Model;
class Post extends Model{
    //Bla bla bla...
}
class Category extends Model{
    //Bla bla bla...
}
class Tag extends Model{
    //Bla bla bla...
}
class Media extends Model{
    //Bla bla bla...
}