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?
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.
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.
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.
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With