How to create a laravel migration file manually

php laravel

1845 просмотра

3 ответа

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?

Автор: Ahmed Essam Источник Размещён: 08.11.2019 11:07

Ответы (3)


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');
Автор: Alexey Mezenin Размещён: 20.08.2016 09:48

1 плюс

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.

Автор: sha-1 Размещён: 20.08.2016 09:59

1 плюс

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.

Автор: Bhavin Shah Размещён: 20.08.2016 09:53
Вопросы из категории :
32x32