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

1.0.0 2017-11-14 02:56 UTC

This package is not auto-updated.

Last update: 2024-05-12 02:24:29 UTC


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");