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');
}
}
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
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
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.
This is a very basic question about laravel.
i have a migration file something like that
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDonorsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('donors', function(Blueprint $table)
{
$table->increments('id');
$table->string('donor_name');
$table->string('email')->unique();
$table->string('blood_group');
$table->string('phone_number')->unique()->nullable();
$table->string('location');
$table->date('date_of_birth');
$table->date('last_date_of_donation');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('donors');
}
}
and i have already migrated my database using this migration file and uploaded it in a live server. now i have to update my migration file and have to add a new field namely donor_image.
how can i do this in a live server?what is the best possible way to update the migration file after uploading a larvel project in a live server?
The best practice is to make another migration file with just the changes (such as adding a column).
Then you run the new migration - which only makes the sub-set of changes listed.
Changing the original migration file is incorrect - and you now cannot use it to apply the changes (without destroying the original table).