dominikstyp/laravel-model-abstractor
Composer 安装命令:
composer require dominikstyp/laravel-model-abstractor
包简介
Makes AbstractModel inside app/models directory, changes make:model command to generate models which inherit from AbstractModel, and provides php artisan command to move your existing models to app/models directory and change their inheritance to AbstractModel. Thanks to that all your models will in
README 文档
README
Makes AbstractModel inside app/models directory, changes make:model command,to generate models which inherit from AbstractModel,
and provides php artisan command to move your existing models to app/models directory and change their inheritance to AbstractModel.
Thanks to that all your models will inherit from your custom class.
Installation
composer require "dominikstyp/laravel-model-abstractor @dev" -vvv
php artisan vendor:publish --provider='\\DominikStyp\\LaravelModelAbstractor\\LaravelModelAbstractorServiceProvider'
Laravel >= 5.5
Due to package discovery feature introduced in Laravel 5.5, you don't have to add service provider to your providers any more.
Laravel < 5.5
For Laravel less than 5.5, you must add service provider to your config/app.php file, as follows:
'providers' => [ // ... DominikStyp\LaravelModelAbstractor\LaravelModelAbstractorServiceProvider::class, // ... ],
Usage
Laravel Model Abstractor provides new console tasks:
laravel-model-abstractor:list-models Lists all your models which inherit from Eloquent\Model
laravel-model-abstractor:change-models-inheritance Changes default models inheritance to AbstractModel,
and namespaces to App\Models, and moves them to app\Models directory.
WARNING! This doesn't affect User model, because it inherits from Authenticatable, so you must change it manually,
along with config/auth.php - providers section.
Example
Let's say you have a model file app/Dummy1.php which contains following code:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Dummy1 extends Model { // }
That model is generated automatically by php artisan make:model Dummy1 command.
But you wish that your model (and others too) would inherit from your AbstractModel class like this:
<?php namespace App\Models; class Dummy1 extends AbstractModel { // }
With your AbstractModel looking like this:
<?php namespace App\Models; /** * AbstractModel * */ abstract class AbstractModel extends \Illuminate\Database\Eloquent\Model { /** your custom code **/ }
All you have to do is invoke php artisan laravel-model-abstractor:change-models-inheritance and you're done.
Additional Features
If you'll look into Models/Traits directory you'll find LocalScopes.php trait, which is attached to AbstractModel. LocalScopes provides you following functionality for all your models which inherit from AbstractModel (look at example below):
DummyModel1::where("something",1)->newest(); DummyModel1::where("something",2)->oldest(); // these are equivalent of DummyModel1::where("something",1)->orderBy("id","desc"); DummyModel1::where("something",2)->orderBy("id","asc");
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-13