Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a laravel migration file manually

Tags:

php

laravel

I'm creating a simple generator that will help me in my job to create models, controllers, views, routes, migrations with simple few lines of YAML. Everything looks great till now but I had a problem and searched a lot about it but still get nothing.

My tool generates a migration file, the thing is the migration file name must follow specific rules so the php artisan migrate command can detect the file and migrate it.

the file name must start with the year, the month, the day and a number then the description of the migration. for example. 2014_10_12_000000_create_users_table.php 2014_10_12_100000_create_password_resets_table.php Now, It's easy to put the date in the beginning of the file, but how can I follow the number rules? the number increases by one every new migration file.

How can I detect the last file created to know its number and make a new migration file with a number larger than the last migration file created?

Meantime I create the migration file name with this code.

$migrationFileName = date('y').'_'.date('m').'_'.date('d').'_'.'create'.ucfirst($name).'Table';

But the command php artisan migrate still cannot detect the file to migrate it without the number. So how can I do that?

like image 266
Ahmed Essam Avatar asked Aug 20 '16 09:08

Ahmed Essam


People also ask

How do I create a new migration file?

To create a new migration, you can run the make:migration Artisan command and that will bootstrap a new class on your Laravel application, in the database/migrations folder. This class will contain a default boilerplate code.

What methods are used in laravel for migration?

A migration class contains two methods: up and down . The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method.


3 Answers

If you want to know what is actually the format for the naming of migrations file you should look at the Illuminate\Database\Migrations\MigrationCreator file. The number you are guessing as an ordering is actually a timestamp which you can find in the following function in that file:

protected function getDatePrefix()
{
    return date('Y_m_d_His');
}

And following is the full filename creation function:

protected function getPath($name, $path)
{
    return $path.'/'.$this->getDatePrefix().'_'.$name.'.php';
}

Hope it helps.

like image 91
sha-1 Avatar answered Oct 21 '22 19:10

sha-1


It's not a number, it's current timestamp, like 143710. So, you could try something like this to get it:

Carbon::now()->format('His');
like image 34
Alexey Mezenin Avatar answered Oct 21 '22 18:10

Alexey Mezenin


It's Laravel's inbuilt mechanism. as soon as you create migration , the naming_convention will look a like timestamps and name of your table.

Or else you may use Laravel Generator. or you can use Carbon Timezone.

like image 43
Bhavin Shah Avatar answered Oct 21 '22 18:10

Bhavin Shah