Automatic Laravel model migrations.

Overview

Laravel Automatic Migrations

Automatic Laravel model 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 perfectly fine alongside traditional Laravel migration files, for the edge 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:

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 {name}

Making Factories

Make a factory whose definition points to a model:

php artisan make:afactory {name}

Running Migrations

Run automatic migrations:

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

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' => base_path('resources/stubs/vendor/laravel-automatic-migrations'),

Now just edit the stub files inside resources/stubs/vendor/laravel-automatic-migrations to your needs. The 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.

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

class User extends Authenticatable
{
    use HasHashes;

    protected $hashes = ['password'];
}
Issues
  • Relations when it comes to seeds

    Relations when it comes to seeds

    One thing i am struggling to figure out how to do relations with the seed data in the definition(). Not sure how i can make that 'contact_id' in the seed related to an actual contact from the Contact model. Will be useful to know for other uses. Right now i just have a hard contact id set of 1.

    <?php
    
    namespace App\Models;
    
    use Bastinald\UI\Traits\HasFillable;
    use Faker\Generator;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Illuminate\Database\Schema\Blueprint;
    
    class Transaction extends Model
    {
        use HasFactory;
        use HasFillable;
        use SoftDeletes;
    
        public function migration(Blueprint $table)
        {
            $table->id();
            $table->unsignedBigInteger('contact_id');
            $table->string('description');
            $table->string('payment_gateway');
            $table->string('gateway_transaction_id');
            // create unique relationship
            $table->unique(['payment_gateway', 'gateway_transaction_id']);
            $table->decimal('amount', 10, 2);
            $table->decimal('fees', 10, 2)->default(0.00);
            $table->timestamp('created_at')->nullable();
            $table->timestamp('updated_at')->nullable();
            $table->softDeletes();
        }
    
        public function definition(Generator $faker)
        {
            return [
                'contact_id' => 1,
                'description' => $faker->words,
                'payment_gateway' => 'square',
                'gateway_transaction_id' => $faker->unique()->numerify('########'),
                'amount' => $faker->randomFloat(2, 10, 20),
                'fees' => $faker->randomFloat(2, 0, 0.4),
            ];
        }
    }
    
    opened by MACscr 2
Easy creation of slugs for your Eloquent models in Laravel

Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you

Colin Viebrock 3.2k Jun 14, 2021
Please see the Diglactic fork.

Update: 18 October 2020 There is now an official fork of Laravel Breadcrumbs: https://github.com/diglactic/laravel-breadcrumbs Blog post: https://news

Dave James Miller 2.4k Jun 17, 2021
An Eloquent Way To Filter Laravel Models And Their Relationships

Eloquent Filter An Eloquent way to filter Eloquent Models and their relationships Introduction Lets say we want to return a list of users filtered by

Eric Tucker 1.2k Jun 18, 2021
Easily create a revision history for any laravel model

Wouldn't it be nice to have a revision history for any model in your project, without having to do any work for it. By simply adding the RevisionableT

Venture Craft 2.2k Jun 20, 2021
This package gives Eloquent models the ability to manage their friendships.

Laravel 5 Friendships This package gives Eloquent models the ability to manage their friendships. You can easily design a Facebook like Friend System.

Alex Kyriakidis 655 Jun 5, 2021
Laravel Ban simplify blocking and banning Eloquent models.

Laravel Ban Introduction Laravel Ban simplify management of Eloquent model's ban. Make any model bannable in a minutes! Use case is not limited to Use

cybercog 614 Jun 4, 2021
Automatically validating Eloquent models for Laravel

Validating, a validation trait for Laravel Validating is a trait for Laravel Eloquent models which ensures that models meet their validation criteria

Dwight Watson 918 Jun 8, 2021
A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

Laravel Moderation A simple Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc. Keep yo

Alex Kyriakidis 490 Jun 9, 2021
This package lets you add uuid as primary key in your laravel applications

laravel-model-uuid A Laravel package to add uuid to models Table of contents Installation Configuration Model Uuid Publishing files / configurations I

salman zafar 10 May 31, 2021
A laravel Livewire Dynamic Selects with multiple selects depending on each other values, with infinite levels and totally configurable.

Livewire Combobox: A dynamic selects for Laravel Livewire A Laravel Livewire multiple selects depending on each other values, with infinite levels of

Damián Aguilar 15 Jun 5, 2021
Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

Laravel Uuid Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for versio

Christoph Kempen 1.7k Jun 16, 2021
Sortable behaviour for Eloquent models

Sortable behaviour for Eloquent models This package provides a trait that adds sortable behaviour to an Eloquent model. The value of the order column

Spatie 947 Jun 11, 2021
User to Team associations with invitation system for the Laravel 5 Framework

Teamwork This package supports Laravel 6 and above. Teamwork is the fastest and easiest method to add a User / Team association with Invites to your L

Marcel Pociot 883 Jun 8, 2021
Log activity inside your Laravel app

Log activity inside your Laravel app The spatie/laravel-activitylog package provides easy to use functions to log the activities of the users of your

Spatie 3.8k Jun 13, 2021