laravel4 update migration file in live server - php

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).

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

Want to add column but migration class is not found even It exists

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

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');
}
}

Importing Mysql statements into Laravel

I'm new to using Laravel.
I have a .sql file where created a whole bunch of tables("CREATE TABLE my_table......."). Is there a way to somehow import these statements into Laravel? I could always manually rewrite these tables in raw php, but I feel that this would take too long and there is possibly an easier way to do it.
You would use migrations in Laravel to do this for example
I want to create a table called tasks I would use this command in artisan
php artisan make:model Task
This would create a model called task and a migration for this table.
You can then access the migration file and input something like this
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('task_id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('tasks');
}
}
You would then run the artisan command to create this table
php artisan migrate
This then automatically creates your table.
Its a very useful feature and you can also rollback your migrations or even refesh it meaning it will remove the table and then reinstate it with no data in.
php artisan migrate:refresh
link to the docs
http://laravel.com/docs/5.1/migrations

Categories