GithubHelp home page GithubHelp logo

balasubrahmanyamd-rt / laravel-automatic-migrations Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fadlee-archive/laravel-automatic-migrations

0.0 0.0 0.0 56 KB

Automatic Laravel model migrations.

PHP 100.00%

laravel-automatic-migrations's Introduction

Laravel Automatic Migrations

Instead of having to create and manage migration files, this package allows you to specify your migrations inside your model classes via a migration method. When you run the migrate:auto command, it uses Doctrine to compare your model migration methods to the existing schema, and applies the changes automatically.

This package works fine alongside traditional Laravel migration files, for the cases where you still need migrations that are not coupled to a model. When you run the migrate:auto command, it will run your traditional migrations first, and the automatic migrations afterwards.

Documentation

Installation

Require the package via composer:

composer require bastinald/laravel-automatic-migrations

Usage

Declare a migration method in your models:

namespace App\Models;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
   public function migration(Blueprint $table)
   {
       $table->id();
       $table->string('name');
       $table->timestamp('created_at')->nullable();
       $table->timestamp('updated_at')->nullable();
   }
}

Run the migrate:auto command:

php artisan migrate:auto

Commands

Making Models

Make a model with a migration method included:

php artisan make:amodel {class} {--force}

This command will also make a factory whose definition points to the model method. Use --force to overwrite an existing model.

Running Migrations

Run automatic migrations:

php artisan migrate:auto {--f|--fresh} {--s|--seed} {--force}

Use -f to wipe the database, -s to seed after migration, and --force to run migrations in production.

Migration Order

You can specify the order to run your model migrations by adding a public migrationOrder property to your models. This is useful for pivot tables or situations where you must create a certain table before another.

class MyModel extends Model
{
    public $migrationOrder = 1;

    public function migration(Blueprint $table)
    {
        $table->id();
        $table->string('name');
        $table->timestamp('created_at')->nullable();
        $table->timestamp('updated_at')->nullable();
    }
}

The migrate:auto command will run the automatic migrations in the order specified. If no order is declared for a model, it will default to 0. Thanks to @vincentkedison for this idea.

Publishing Stubs

Use your own model and factory stubs by publishing package files:

php artisan vendor:publish --tag=laravel-automatic-migrations

Update the stub_path in config/laravel-automatic-migrations.php:

'stub_path' => resource_path('stubs/vendor/laravel-automatic-migrations'),

Now edit the stub files inside resources/stubs/vendor/laravel-automatic-migrations. Commands will now use these stub files to make models and factories.

Traits

HasHashes

This trait will automatically hash attributes specified via a $hashes property in your model. It will only do so if the values are not already hashed, so it does not slow down seeders.

namespace App\Models;

use Bastinald\LaravelAutomaticMigrations\Traits\HasHashes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasHashes;

    protected $hashes = ['password'];
}

laravel-automatic-migrations's People

Contributors

bastinald avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.