vdauchy / laravel-model-injector
0.2.0
2021-03-20 22:24 UTC
Requires
- php: ^7.4|^8.0
- illuminate/database: *
- illuminate/support: *
This package is auto-updated.
Last update: 2025-04-21 07:33:43 UTC
README
Laravel Model Injector
This package is a simple trait to help on Model Injection.
The main goal is to replace class set in config as in laravel-permission:
// In: config/permission.php return [ 'models' => [ 'permission' => Permission::class, 'role' => Role::class, ], ... ]; // In: src/PermissionRegistrar.php public function __construct(CacheManager $cacheManager) { $this->permissionClass = config('permission.models.permission'); $this->roleClass = config('permission.models.role'); ... }
And instead do:
// In: src/PermissionRegistrar.php use HasModelInjection; public function __construct(CacheManager $cacheManager) { $this->permissionClass = $this->mClass(Permission::class); $this->roleClass = $this->mClass(Role::class); ... }
To use their own class, any developer can do:
$this->app->bind(Permission::class, MyCustomPermission::class);
Current API:
mQuery(string $model): Builder
: Let you build a query against this Model.
mNew(string $model, array $attributes = []): Model
: Let you create a new instance of this Model.
mClass(string $model): string
: Return the class associated to the Model.
mTable(string $model): string
: Return the table's name of the Model".
mColumns(string $model): Collection
: Return a Collection of columns.