Laravel Schema builder drop table on specific connection - php

I created a users table using Laravel's schema builder. Here is the migration:
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('mysql-internal')->create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('mysql-internal')->drop('users');
}
}
After running this migration, I then rolled it back using php artisan migrate:rollback. However, it looks like down method did not respect my connection. The users table was dropped from my default mysql database rather than the mysql-internal that I specified.
Did I do something wrong here? I am using Laravel 4.2.17.

Related

Laravel 8 - Change existing migration

I am developing on Laravel Framework 8.33.1 and have the following migration on my local environment and also in production.
class CreateCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('company', function (Blueprint $table) {
$table->id();
$table->integer('company_id');
$table->integer('messageId');
$table->string('url');
$table->timestamps();
/**
New table:
$table->id();
$table->integer('company_id')->nullable($value = true);
$table->integer('messageId');
$table->integer('people_id')->nullable($value = true);
$table->string('url')->nullable($value = true);
$table->timestamps();
*/
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('company');
}
}
I would like to change the migration with the following adapted fields:
$table->id();
$table->integer('company_id')->nullable($value = true);
$table->integer('messageId');
$table->integer('people_id')->nullable($value = true);
$table->string('url')->nullable($value = true);
$table->timestamps();
As I am using the current migration in production I do not want to lose data.
I simply tried to modify the migration file with my new table definition, but I get:
> php artisan migrate
Nothing to migrate.
Any suggestions how to change the migration properly in laravel?
I appreciate your replies!
To modify an existing table, create a new migration.
php artisan make:migration alter_company_table
class AlterCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable()->change();
$table->integer('people_id')->nullable();
$table->string('url')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable(false)->change();
$table->dropColumn('people_id');
$table->string('url')->nullable(false)->change();
});
}
}
If you don't mind losing data and you are in development mode, you can do php artisan migrate:rollback and after execute: php artisan migrate. So your migrations works correctly. You can to read more here.
Now, if you just want to add new tables and modify others in you database, you should make another migration. But before this, you should to install doctrine/dbal to be able to modify your tables correctly, or else, it will give you many errors. After, add this lines in your config/database.php
use Illuminate\Database\DBAL\TimestampType;
'dbal' => [
'types' => [
'timestamp' => TimestampType::class,
],
],
Now you can do: php artisan make:migration add_new_tables_to_company and your file in database/migrations.
class AlterCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable()->change();
$table->integer('people_id')->nullable();
$table->string('url')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable(false)->change();
$table->dropColumn('people_id');
$table->string('url')->nullable(false)->change();
});
}
}

Laravel 5 - Add column and foreign key to an existing table with data

I am very new at Laravel! But I'm trying. Here is were I could use some help;
I have a posts table. I have a user table. But I forgot to add a foreign key in the posts table that links to the user id.
The create users table migration:
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::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
The create posts table migration:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
I have created the new migration file:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterTablePostsAddForeignUser extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
// I HAVE NO IDEA WHAT TO DO NEXT
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
I have no idea how to fill up the "public function up()" method here! If anyone can help! It will be much appreciated!
I tried this and this worked for me.
Schema::table('posts', function (Blueprint $table) {
$table->integer('user_id')->nullable()->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
Here When you adding foreign key, by default it will have a Null value.This 'null referencing' may cause the issue. Using nullable() foreign key will avoid that issue.
See the docs here
You need to install doctraine/dbal before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column and the you should try what I found on this SO post
Schema::table('posts', function (Blueprint $table) {
$table->integer('user_id')->unsigned()->change();
$table->foreign('user_id')->references('id')->on('users');
});
change() method for change structure of column
after this run the artisan command
php artisan migrate
if this doesn't work for you shoot the comment here! :)

How to check database table which has been created in laravel 4.2?

I'm new in laravel, anyone know how to see database table which has been created? I've been migrate the database and stuck after create table using schema builder. This is my code
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('nim');
$table->string('name');
$table->string('password');
$table->string('level');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
The new table name will be "users" because you have given 'users' as the name in the Schema::create function.
You can view the table from phpmyadmin if you are using the mysql database, which is sat as the default database for laravel.

Laravel migration dropColumn drop the wrong column

I've a very weird issue i cannot explain. This make me worrying a lot because the project i'm working on is online and I might update the database columns sometimes.
// Migration file
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->datetime('status_updated_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('user_profiles', function($table)
{
// Columns to remove
$table->dropColumn('status_updated_at');
});
}
}
Now I migrate, everything works well i checked and my table is the good one
html(master)$ php artisan migrate
Migrated: 2015_03_19_111236_change_user_profiles_table
Here's a part of my MySQL table
Now I rollback, as simple as it sounds.
html(master)$ php artisan migrate:rollback
Rolled back: 2015_03_19_111236_change_user_profiles_table
And now I just look at my table and it gets freaky
Yes, the status column vanished for no reason. And there's still the one that was supposed to be removed. I tried 10 times it just doesn't work ... I cannot even migrate anymore because
[Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'status_
updated_at' (SQL: alter table `user_profiles` add `status_updated_at` datet
ime null)
Someone has an idea about it ? If it's an issue, it's a BIG one because migrations are really sensitive stuff within a project ... I don't really trust Laravel anymore and I wonder how I'll do with the production side.
**EDIT : To find a solution here are all the migrations linked with this table (but it shouldn't be called anyway ...)
I'm using Laravel 4.2**
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('user_profiles', function($table)
{
// Keys
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->integer('box_id')->unsigned()->nullable();
$table->string('stripe_customer');
$table->string('contract_id');
// Indexes
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('box_id')->references('id')->on('boxes')->onDelete('cascade');
// Timestamps
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
$table->dropForeign('user_profiles_user_id_foreign');
$table->dropForeign('user_profiles_box_id_foreign');
});
Schema::dropIfExists('user_profiles');
}
}
And later
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->enum('status', array('not-subscribed', 'in-progress', 'subscribed', 'expired'));
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
// Columns to remove
$table->dropColumn('status');
});
}
}
The fact I had the same name of classe in some migrations put composer in trouble. A composer dump-autoload allowed me to see it.
For instance :
Generating autoload files
Warning: Ambiguous class resolution, "ChangeBoxQuestionsTable" was found in both "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_12_183836_change_box_questions_table.php" and "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_19_040137_change_box_questions_table.php", the first will be used.
So I changed manually my files / classes names and also in the database migrations table too. Now it works fine ;)

Laravel 4 - one migrations in workbench can be rolled back, while others cannot

I have two types of workbench migrations: creating regular tables and creating pivot tables for Many-to-Many relationships.
Sample of regular migration:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('login')->unique();
$table->string('username')->unique();
$table->string('password');
$table->string('email');
$table->boolean('active')->default(true);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Above migration can be rolled back
<?php
use Illuminate\Database\Migrations\Migration;
class CreatePivotRoleUser extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('role_user', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->primary(['role_id', 'user_id']);
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
While this cannot, because it gives
"Class 'CreatePivotPermissionRole' not found"
error.
How to fix it?
Your code looks correct.
If CreatePivotPermissionRole is not found, it means it had been deleted before. Check the content of all your down() methods, there must be something wrong there.

Categories