I created an app in Laravel. In the beginning, I made a migration with the following content:
public function up()
{
Schema::create('kundens', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('user_id');
$table->string('vorname');
$table->string('nachname');
$table->string('strasse');
$table->integer('plz');
$table->string('wohnort');
$table->string('mail');
$table->integer('telefon');
$table->string('geburtsdatum');
});
}
No I want to add some tables like kaufpreis or "modernisierung". I added them under the other tables but when I save the file and write in the terminal I get the error:
nothing to migrate.
So now how can I add some tables for more information?
You should create a new migration for kaufpreis and modernisierung.
The main migration for kundens did already run (see migrations table).
php artisan migrate:fresh is also an option, if you are developing locally.
Don't do this when you work with other people / production as it will erase the tables and create new ones (data will be lost)
Related
I have table and I wanted to update on some columns, or if I wanted to add new column the problem is when I want to use php artisan migrate command gives me error table already exist, also Im using depoly file and the command inside it is php artisan migrate --force so hope this is correct or have to add any more command??
public function up()
{
Schema::create('payment_methods', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->string('paymentreference')->unique();
$table->string('payment_token');
$table->string('cardnumber'); //updated
$table->string('cardbin')->nullable();
$table->string('cardlast4');
$table->string('cardtype');
$table->string('expirymonth');
$table->string('expiryyear');
$table->string('cardholdername'); //added
$table->timestamps();
});
}
To added new or update field like profile in payment_methods.
Try
Run command:
php artisan make:migration add_profile_to_payment_methods
And in the up() method of the new migration file generated, use Schema::table() method to add the new columns or modifying the table.
public function up()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->string('profile')->nullable();
});
}
public function down()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->dropColumn('profile');
});
}
}
Then run migration to update the table using php artisan migrate
You need a new migration to modify existing table.
Create new migration:
php artisan make:migration modify_payment_methods_table
Then open the migration file and put following code in there:
public function up()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->string('cardnumber')->change();
$table->string('cardholdername');
});
}
public function down()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->integer('cardnumber')->change(); // todo: if this was not an integer then fix this to be correct type instead of integer to avoid issue in case if you will have to rollback the migration
$table->dropColumn('cardholdername');
});
}
After this run
php artisan migrate
To do this successfully you may need to install additional dependency doctrine/dbal.
You can install that easily with composer:
composer require doctrine/dbal
You have manipulated or an error has occurred in any of the migrations.
Well, now in the migrations table, there isn't a row that contains create_payment_methods_table in the migrations column.
As it does not exist, but the table to which the migration refers, if it exists in your database, it fails you, since the process is as follows:
When you refresh, Laravel reads the migrations table, and executes
each migration file in order, first executing the down or deletion of
the table.
After executing that step in all migrations, go through the UP. When
the down of that table does not exist, when arriving at its demo
file, the up finds that it already exists. And that's why it fails
you.,
The solution is to delete manually the referenced table and rerun the migration
I'm new to laravel framework. For making a blog URL's to SEO friendly, I need to add an extra column to the existing blog tables for the laravel website. Can we directly add a column to a table directly in the database or not? Can we add a column without commands or migrations? Would you please suggest an easy method to add the column?
Add migration
php artisan make:migration add_fieldname_to_tablename
Code methods migration
public function up()
{
Schema::table('tablename', function (Blueprint $table) {
$table->datatype('column_name')->nullable();
});
}
public function down()
{
Schema::table('tablename', function (Blueprint $table) {
$table->dropColumn('column_name');
});
}
Run migration
php artisan migrate
Better is to add at migration level but if you want to directly add at DB level that is also an option. But update migration as well so that it will have all the columns.
I've inserted a column with name im_useless to my table earlier which I do not need anymore.
This is my schema (filename: 2017_02_27_120313_units.php):
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->string('im_useless');
$table->timestamps();
});
}
Now I try to remove it, so I used this code inside the down() function:
public function down()
{
Schema::dropColumn('im_useless');
}
New Schema:
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
Now I have to rollback and then migrate again. I try to rollback only that specific migration file, by executing php artisan help migrate:rollback I found out that there is a --path option.
So I tried to rollback that specific migration like this:
php artisan migrate:rollback --path=2017_02_27_120313_units.php
But I get Nothing to rollback
How can I drop that specific column without having to rollback any other migrations?
UPDATE:
I think I have to change the path like this:
php artisan migrate:rollback --path=database/migrations/2017_02_27_120313_units.php
...since my php shell was opened in the project root folder?
However I still get Nothing to rollback
I also tried php artisan migrate --path=2017_02_27_120313_units.php
and php artisan migrate --path=database/migrate/2017_02_27_120313_units.php
...and get Nothing to migrate
UPDATE 2
I think I have messed up my migrations table, because I removed the code inside the down() function and the table was never deleted.
https://stackoverflow.com/a/26077506/4684797
The rollback function is meant to give you the possibility to revert to the version you had right before you migrated, in case something goes wrong when you deploy. If you want to drop a specific column that you don't need anymore, you should treat that as a new migration and drop the column in the up() method.
If your laravel version is >= 5.3 you could simply add the migrations path like packages does.
https://laravel.com/docs/5.3/packages#migrations
Just put this code in your AppServiceProvider boot method:
$this->loadMigrationsFrom(__DIR__.'/path/to/migrations');
Also if you want to include subfolders recursively you can try with "/path/to/migrations/**/*" but I'm not sure if this will work in older laravel versions.
--path param is like a filter. It will work on the latest migrations that rollback will act. You can use path reference starting from datatabase/migrations. For example:
php artisan migrate:rollback --path=database/migrations/2022_10_03_193316_create_something.php
It will work if in your database, you have something like 2022_10_03_193316_create_something.php on migration column plus greatest batch value.
Tested on Laravel 6
Run composer dump-autoload and try again
I am using Laravel 5.2. I have created basic tables using migration method. But now I need to add another extra table. After creating my schema, I have given php artisan migrate command. But it shows error like base table or view already exists Table:Users. I know why this happen. The migration command trying to recreate the table which already have. But I need to add another extra table in Laravel via Migration. I have gone through this https://laravel.com/docs/5.2/migrations But I can't get any solution.
If you want to create another table, just create new migration and run it.
If you're trying to add columns into existinng table, use Schema::table instad of Schema::create.
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
}
Schema::table('articles', function (Blueprint $table) {
$table->string('description');
}
If you already executed php artisan migrate then next time it will give you error saying "Table already exists.".
So if you want to execute only a particular migration then either you can temporarily move all migration's php file which are executed, out of database/migrations folder and then execute
php artisan migrate
or
you can execute migration from tinker i.e. first execute php artisan tinker and then execute content of up method from the migration but without parameter type Blueprint.
Eg.
If following is your migration up method content
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
});
then you will have to execute
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
});
I came from Django(Python) background and these days I'm working on a project which is based on Laravel(PHP).Do I have some option like generating database tables automatically?
Yes, using the Schema Builder and Migrations.
First you need to install the migrations table to the DB:
$ php artisan migrate:install
then create a migration
$ php artisan migrate:make create_users_table
this will create a PHP file in application/migrations. You may now edit it to have the settings you want, i.e.
<?php
class Create_Users_Table
{
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('username');
$table->string('email');
$table->string('phone')->nullable();
$table->text('about');
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
and execute it using
$ php artisan migrate
Every time you change the database structure you'll have to create a new migration and execute it afterwards.
Say you want users to have a new column hometown instead of phone you'd create a new migration
$ php artistan migrate:make users_table_add_hometown
and edit the new file to contain
<?php
class Users_Table_Add_Hometown
{
public function up()
{
Schema::table('users', function($table)
{
$table->string('hometown');
$table->drop_column('phone');
});
}
public function down()
{
Schema::table('users', function($table)
{
$table->string('phone')->nullable();
$table->drop_column('hometown');
});
}
}
You now have two migrations, one creating the table and one modifying it.
The artisan migrate command is smart enough to only execute migrations that are new to the system. So if a collegue of yours comes home after a long vacation and there were a few new migrations it will automatically only import the ones that were created after he left.