Laravel Migrations - php

I am new to laravel. I have created migrations and I can see them in my database/migrations folder. But when I run the php artisan migrate command, it throws this error:
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
Yes, I have already migrated the user table already, but now I have created another migration. Isn't it supposed to migrate the last migration I have created after my php artisan migrate command?

If your second migration for the users table looks like this:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 25);
// more migrations ...
});
it will try to create the users table again. Note the second migration has Schema::create. You are looking to do something more like alter the table:
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->change();
});
If you are looking to completely overwrite (this will delete all data in the table) your prior migration with a new table you could do something as follows:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::dropIfExists('users');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
However, if you are doing this you are most likely misunderstanding how migrations work. The purpose is to be able to alter columns in your database without having to completely destroy the table.

Related

Laravel Migrations breaks when migrating new tables into the database

Iam trying to migrate Laravel table migrations into the database but every time I make new tables and run php artisan migrate Laravel complains that user table exists and even the newly added migrations are not created in the database, and when I run php artisan migrate:fresh I lose my data is there a better way to do this without losing my data in the already existing tables. Below is the table i want to add.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up() {
Schema::create('drivers', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('contact')->unique()->nullable();
$table->string('code')->unique();
$table->string('nin')->nullable();
$table->date('birthday')->nullable();
$table->string('image')->nullable();
$table->bigInteger('country_id')->nullable();
$table->boolean('verified')->nullable()->default(false);
$table->timestamps();
$table->foreign('country_id')->references('id')->on('countries');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down() {
Schema::dropIfExists('drivers');
}
}
Is it perhaps saying that the Class CreateUsersTable already exists?
I can see that the class name is CreateUsersTable, but you should probably rename to CreateDriversTable

Laravel 5.6 migrations

I have a problem with migrations in a Laravel 5.6.
This is a problem:
My code in a Laravel:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('companies');
}
}
What i need to do?
Table already exists as it is telling you in the message, you may need to drop it first or check that you're using the right database name in your ENV file. Also check you're not creating the table companies in a previous migration.
If you're trying to add fields to an existing table you don't need the create method but:
Schema::table("companies", function (Blueprint $table) {
// The fields you need
});
Also for the foreign key is safer to use unsignedInteger as data type
As shown in the error message "companies" table already exist.
you can use following commands to resolve it.
php artisan migrate:rollback
php artisan migrate:reset
please refer the laravel documentation for more details.
You can use below command to drop every table and migrate before that you have to take a backup of your sql because it deletes every data
php artisan migrate:fresh

How does Laravel's migration up and down method work?

In this code the table is created in the up method and deleted in the down() method. When I run the migration, the table is created but not deleted. In what way can I trigger the down method, so that I get a better understanding on how the two methods work?
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlightsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('flights');
}
}
In your example:
php artisan migrate will run your up() method.
php artisan migrate:rollback will run your down() method.
Read up on the excellent docs: https://laravel.com/docs/5.7/migrations#migration-structure
You sohuld add dropIfExists instead of drop
and if you wanna just drop the specific migration file you should write your code in command like this:
php artisan migrate:rollback --path=\database\migrations\flights.php
Try:
public function down()
{
Schema::dropIfExists('flights');
}
Basically, up() is for creating tables or altering tables in the database, and down() is for reverting those changes.
Note: both of these methods create your own logic that how you want to go about making changes.
calling these methods:
up() php artisan migrate
down() php artisan migrate:rollback

Laravel: How to create migration file for drop multiple columns from a table?

When I drop a column, I use following command to create migration file,
php artisan make:migration drop_institue_id_column_from_providers
And migration file:
public function up()
{
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id');
});
}
public function down()
{
Schema::table('providers', function (Blueprint $table)
{
$table->integer('institue_id')->unsigned();
});
}
If I want to drop multiple columns, I can write migration file this way,
public function up()
{
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id');
$table->dropColumn('institue_name');
$table->dropColumn('institue_address');
});
}
public function down()
{
Schema::table('providers', function (Blueprint $table)
{
$table->integer('institue_id')->unsigned();
$table->char('institue_name');
$table->char('institue_address');
});
}
But What is the command to create that migration file? Thank you.
I assume this is just a question about naming convention for the migration class.
If so then it does not matter what the file\class is called, as long as it gives you a good idea of what it does.
So it could be something like:
php artisan make:migration drop_institue_columns_from_providers
When you execute the php artisan make:migration command, it just create a class in which you can specific what migration you can do. You can do many modification for one table, even for many table. So just put all your code in one migration file will be OK.
To create a migration file run:
php artisan make: migration your_migration_file_name_here
and then you can drop multiple columns by passing an array
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id', 'institue_name', 'institue_address');
});

Laravel 5.1 - make migrations not making any tables in the MySQL

I am not seeing any tables apart from Users table created in MySQL DB while using Laravel migrations. I have followed the steps given below
Laravel version : 5.1
1) set timezone to Asia/Kolkata
2) Added following files into Apps/Model
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCardsTable extends Migration
{
public function up()
{
Schema::create('cards', function(Blueprint $table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('pin', 10);
$table->string('location',256);
$table->datetime('daterequested');
$table->datetime('datefulfilled');
$table->integer('hitcount');
$table->integer('fulfilledby');
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::drop('Cards');
}
}
3) Ran the command
php artisan make:migration create_cards_table
It displayed the message 'Created migration'
4) Ran the command php artisan migrate
5) No table is created in MySQL.
FYI, The only db configuration i made is in .env file . I haven't added anything to database.php in config directory (even though i edited username other db related information in the mysql array to match my own credentials)
Please let me know what am i doing wrong here.
First you need to create the migration file this way
php artisan make:migration create_cards_table
it will create a file prepended with date inside database->migrations folder.
Edit that file the way you have done but remember it is not Cards but cards in the delete down function. Also remember in dateTime T is in capital. It is case sensitive. Finally you will have this code
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCardsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::create('cards', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('pin', 10);
$table->string('location',256);
$table->dateTime('daterequested');
$table->dateTime('datefulfilled');
$table->integer('hitcount');
$table->integer('fulfilledby');
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
Schema::drop('cards');
}
}

Categories