why can not php artisan migrate laravel - php

when I tried php artisan migrate an error :
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'laravel.users' (SQL: drop table `users`)","file":"\/opt\/lampp\/htdocs\/laravel\/coba1\/latihan3\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":625}}
I use the mysql database, please give solution

You are attempting to drop a table that does not exist. You are either not using the correct database (laravel) or you are doing this as part of a rollback or modification.
Remember that your migrations should include a function that makes changes (up) and a function that undoes those changes (down). Database: Migrations
public function up()
{
Schema::create('users', function (Blueprint $table) {
// columns
});
}
public function down()
{
Schema::drop('users');
}
If you are dropping a table that you are not sure exists you can
Schema::dropIfExists('users');

Check database name (it should be 'laravel' named or change yor config file to right database name) and check existing table users in your database.

Related

laravel, update table structure but get error table already exist when use php artisan migrate

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

How to add column to existing table in laravel?

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.

Add new columns to existing table in a migration in Laravel

I want to add some new columns in my existing table users in laravel.
I have already googling for that and following those search I have already created migration using the command php artisan make:migration add_columns_to_users.
add_columns_to_users.php
public function up()
{
Schema::table('users', function($table) {
$table->string('address');
$table->string('city');
$table->string('tribe');
$table->string('country');
$table->integer('student_id');
$table->string('tribe_university_name');
$table->string('student_program_of_study');
$table->string('faculty');
$table->string('level');
});
}
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('address');
$table->dropColumn('city');
$table->dropColumn('tribe');
$table->dropColumn('country');
$table->dropColumn('student_id');
$table->dropColumn('tribe_university_name');
$table->dropColumn('faculty');
$table->dropColumn('level');
});
}
After creation, I run this command php artisan migrate.
But got the same error:
Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8 collate utf8_unicode_ci)
Full name of user table 2014_10_12_000000_create_users_table.php and the other name is 2019_04_11_074552_add_column_to_users.php
How to solve this?
My main query is How to add new columns in my existing table?
If you check at the error trace:
Base table or view already exists: 1050 Table 'users' already exists
This means that the users table already exists so when you run your migrations it is trying to create a table that is already created in your database.
Note: Don't forget to backup your database first
Delete users table from the database also delete users entries from migrations table.
After, execute the migrate Artisan command:php artisan migrate
Now another your Question is: How to add new columns in my existing table?
You have to create a table using this command:
php artisan make:migration create_users_table
The output you got it like this: Created Migration: 2019_04_12_070152_create_users_table
Your Migration structure is something this:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Now you want to add new columns in your existing users table
php artisan make:migration add_phone_number_to_users_table --table=users
use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('phonenumber')->after('name'); // use this for field after specific column.
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('phonenumber');
});
}
After, you can run your migrations: php artisan migrate
Your new columns(phonenumber) are now added to your existing users table, which you can view in your database.
If you have still any doubt, see this video
The problem comes from the php artisan migrate that will try to migrate both files when 2014_10_12_000000_create_users_table.php is already migrated, so IMO two possible solutions here :
Rollback the users table from the DB and rerun the migrate cmd.
Add the migration name inside the migrations table so the cmd will not try to run it for the second time.
Modifying current migration wont work, because it's entry in migration table is already there. So to make any changes in already existing table, you need to create new migration script.
// remember `create` replaced by `table` here
Schema::table('users', function (Blueprint $table) {
// add whichever new columns you want to
});
Follow these steps,
php artisan make:migrate modify_user_table
open modify_user_table file in database/migrations directory
Add new columns as at top I wrote.
Now save the file after adding new columns into new migration file
cmd -> php artisan migrate
EDIT
If there is no user data then Open 2014_10_12_000000_create_users_table.php file and add Schema::dropIfExists('users'); before Schema::create('users'... line.
If there is data then you can take a backup, again follow the above step 1.
You need do little modification in your artisan command
artisan make:migration add_columns_to_users_table
You then need to use the
Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this
public function up()
{
Schema::table('users', function($table) {
$table->type('column');
});
}
add the rollback option:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('column');
});
}
Then you can run your migrations:
php artisan migrate
Please do the step 1.
php artisan migrate:reset
Step 2: Go to your database using PHPmyadmin (or similar) and delete all remaining tables
including the migration table.
After all please do Step 3 php artisan migrate
You probably got the error because you tried to create another Users table, which is already exist on your database. That could be the reason why you find error Base table or view already exists: 1050 Table 'users' already exists.
So, the solution is try to alter you existing Users table, instead to run another syntax to create and override Users table. By create another alter class inside your data migration folder. It used to be inside your modules folder on your Laravel Project (../database/migrations/).
After you find the directory, create new alter class. For example, alter_users_table.php.
So, if you would like to add another column (e.g.: age column, with type int, nullable) on your Users table, you could code like this on alter_users_table.php:
class AlterUsersAddAge extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('Users', function (Blueprint $table) {
$table->int('age')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('Users', function (Blueprint $table) {
$table->dropColumn('age');
});
}
}
After that, try to run php artisan migrate on your terminal.
Now you should see another new column age on your Users table.

How to drop column with laravel migration on sqlite?

I want to write test for my application, but i getting error when run the migration;
My Migration file
Schema::table('bill_payments', function (Blueprint $table) {
$table->dropColumn('attachment');
// $table->getColumns(); return empty array
// $table->dropColumn(['attachment']); I tried this
});
dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false
// and this is not working because return false.
if(Schema::hasColumn('bill_payments', 'attachment'))
{
Schema::table('bill_payments', function (Blueprint $table) {
$table->dropColumn('attachment');
});
}
Also i add doctrine/dbal 2.5.13
i running tests using mysql, but slowly.
[Solved]
Wow! i using prefix for tables. i deleted this and now it's work.
One thing you need to know about migrations is that they run untill they crash or succeed, following your example:
Schema::table('bill_payments', function (Blueprint $table) {
$table->dropColumn('attachment');
});
//It will drop the column and stop here. When you run the migration again, it will output your error because the column no longer exists.
dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false
What you should have in your migration code is having the reverse operations in the Down() method. Meaning you run the migration, it applies the Up() and when you rollback, it reverts correctly. That error is really what it means, it means that when it reaches an operation relating table bill_payments and column attachment, it recognizes that attachment doesn't exist.
Edit:
There is something related to SQlite in the documentation:
"Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported."

Add Not Nullable column to laravel migration with existing DB data

I have to add a new column to an existing table that already has data in it and running into a bit of a hiccup.
How can I add this column? Its going to be a Not Nullable field. I'm ok with populating it with default data for right now and going back and updating later. so if we need to drop constraints while adding. I'm assuming I'm going to need to utilize straight SQL queries.
Make this work w/ PHPUnit and SQLite, currently I'm getting an error SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL (SQL: alter table "tracks" add column "short_description" text not null)
How would I modify this migration?
public function up()
{
Schema::table('tracks', function(Blueprint $table)
{
$table->text('short_description')->after('description');
});
}
You have to set default value:
public function up()
{
Schema::table('tracks', function(Blueprint $table)
{
$table->text('short_description')->after('description')->default('default_value');
});
}

Categories