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
Related
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
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
I am learning Laravel.
Here is my migration file code.
class CreatePostTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('post', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned(); // i want to add this column after adding this line i runs the command refresh but it shows below errors.
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('post');
}
}
Now I have a problem that whenever I run this command in terminal in PhpStorm:
php artisan migrate:refresh
it shows following errors:
PHP Fatal error: Class 'AddIsAdminColumnToPostTable' not found in C:\xampp\htdocs\cms\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php on line 335
Symfony\Component\Debug\Exception\FatalErrorException]
Class 'AddIsAdminColumnToPostTable' not found
I tried composer dump-autoload in terminal solution from here but it's not working. I also used rollback command but still having issue.
How can make refresh this?
Artisan looks for migrations based on the file name. If you want it to be called something else: rollback, delete the migration, make a new migration. Or, change the file name to exactly match the class name.
For you, try changing
class CreatePostTable extends Migration
to
class AddIsAdminColumnToPostTable extends Migration
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');
}
}
i have just started working with laravel 5.2.. this is a simple migration file but when i run the php artisan migrate command i get the error shown on the screenshot. what should i do now?
migration file
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductCategoryTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('product_category', function (Blueprint $table) {
$table->increments('id');
$table->string('product_category_name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('product_category');
}
}
You need to change Schema::table to Schema::create.
For me, it was because i was dynamically registering Scheduled Tasks in Kernel.php via my own custom Task object by loop over Task::all(). But my 'task' db table wasn't created yet causing the error. so I commented out the Kernel.php code, ran the php artisan migrate command then uncommented my Kernel.php code.
Not sure, but may be best to just do a try{ dbCode; } catch{doNothing;} in the Kernel.php code.