I have used the php artisan migrate:make add_something_to_to_user_table --table=users
and coded
Schema::table('users', function(Blueprint $table)
{
$table->string('description1');
$table->string('description2');
$table->string('description3');
});
and added three fields and gave php artisan migrate and the fields got stored in to the database
also found that the migration table is updated with the row 2014_11_05_145536_add_something_to_to_user_table
now when i use php artisan migrate:rollback
The 2014_11_05_145536_add_something_to_to_user_table row in the migration table is missing but the columns added to the users table remains the same
why it is not deleting the fields in the table also which results in error while using php artisan migrate again...
You should have a down() method in you migration that should look like this:
public function down()
{
Schema::table('users', function($table)
{
$table->dropColumn(array('description1', 'description2', 'description3'));
});
}
That will be called on rollback and will take care of removing the columns added by the migration.
According to laravel 7+ doc this will also work when you need to drop multiple columns at the same time.
public function down(){
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['description1', 'description2', 'description3']);
});
}
Add down public function to drop users table when rollback..
public function down()
{
Schema::drop('users');
}
Related
Is this query is correct?
I'm using Laravel 8, and I want to drop a column in a table. But this query isn't working.
php artisan make:migration remove_contact_no_from_customer --table=customer
you should use To drop a column, you may use the dropColumn method on the schema builder blueprint:
first, you can create a migration file:
php artisan make:migration remove_contact_no_from_customer --table=customer
then, in that migration, make sure you code will be:
Schema::table('customer', function (Blueprint $table) {
$table->dropColumn('contact_no');
});
To drop a table in laravel, Create a first migration
Step to drop a table
$ php artisan make:migration drop_user_table
Add this to your migrate file inside up function Schema::drop('tableName');
public function up()
{
Schema::dropIfExists(table('songs'));
$table->increments('id');
...
}
then run
$ php artisan migrate
That's not a query. It only creates a migration file in your database/migrations folder where you have to add some code like so:
public function up() {
Schema::table('customer', function (Blueprint $table) {
$table->dropColumn('contact_no');
});
}
Afterward, just run php artisan migrate to apply your new migrations to the database, which will drop this column.
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 want to add another column to my database table users. I made a new migration named Modify_users_tablewhich has the code to add the column. There is nothing wrong with the source code because I used it before, but it gives me an error that something is wrong with the table roles? How can I solve this? Here is the error and the code that is related to the error
Here is the migration Roles:
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->string('role_name')->length(55)->unique();
$table->increments('id');
$table->timestamps();
});
}
You've got a number of problems. Your main problem is that you aren't adding a column to a table. Your script is instead trying to create a new table, as seen in this line:
Schema::create('roles', function (Blueprint $table) {
You said you are trying to add a column to the users table, but you aren't even working with the user's table: you are instead creating a table called roles which already exists. The simplest solution is to do what Dhaval suggests: ditch the migration you are added, update the migration that creates the user tables, nuke and start over.
The "right" answer is to create a new migration and put in the instructions to add the column to the users table as well as instructions on removing it. Then you can just do a simple migration instead of nuking your database. That would look something like this:
public function up()
{
Schema::table('users', function ( Blueprint $table ) {
$table->string('last_name')->after( 'first_name' )->default( '' );
});
}
public function down()
{
Schema::table( 'users', function ( Blueprint $table ) {
$table->dropColumn( ['last_name'] );
});
}
You need to run
php artisan migrate:rollback
if that also fails just go in and drop all the tables which you may have to do as it seems your migration table is messed up or your roles table when you ran a previous rollback did not drop the table.
or you can use migrate:reset command will roll back all of your application's migrations:
php artisan migrate:reset
I had the same issue and I tried something like this and it works perfect for me. My Laravel version is 5.5.
public function up()
{
Schema::connection('mysql2')->create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('image');
$table->timestamps();
});
}
public function down()
{
Schema::connection('mysql2')->dropIfExists('images');
}
I had to connections in my project one is mysql and other is mysql2 becuase my project is for database with multiple connections.
So my solution is that to try specifying the connection to your database in the function.
By default connection is mysql if you havent specifed it in the function, Example is given below. Hope this helps!
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email',60)->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
And so when you migrate using php artisan it doesn't show errors,
remove the table from the database ( I did it manually the first time I had this issue)
and make this change in the create_users_table migration as such
$table->string('email', 60)->unique();
and this worked for me..
Hope this helps you as well
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.