Cannot drop column of existing table in Laravel 5.3? - php

I am having a problem in Laravel 5.3.
It won't allow me to drop a column from an existing table. I have run 'composer require doctrine/dbal' and that worked fine, but my column will not delete.
My add_column_to_table code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnToTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table)
{
$table->string('avatar')->default('default.pngs');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function ($table)
{
$table->dropColumn('avatar');
});
}
}
Thanks

To drop the column you need to rollback the migration in the following manner
php artisan migrate:rollback

Try this:
public function down()
{
Schema::table('users', function (Blueprint $table)
{
$table->dropColumn('avatar');
});
}
}

You should rollback your migration:
php artisan migrate:rollback
But make sure that column is empty on your table.

Related

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories")

I just deployed on Heroku yesterday and connected to a Postgresql db and since then I have this fun error showing up on my screen (and terminal) on Heroku:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories")
In my terminal, below this error, I have an Undefined table error stating that my categories table doesn't exist. This so frustrating because it does exist and it's right there! Can someone help with this? Has anyone had a similar Issue?
Tried:
Rollback tables: heroku run php artisan migrate:rollback
migrate fresh: heroku run php artisan migrate:fresh
migrate reset: heroku run php artisan migrate:reset
The migrations run up until the stories table where the relationship is located and then stop running. Right below stories is the categories table. I don't know how much this helps in fining a solution.
stories table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('stories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('story');
$table->date('published_on');
$table->integer('count_views')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger("category_id");
$table->foreign('category_id')->references('id')->on('categories')->ondDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('stories');
}
}
categories table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category');
$table->string('title')->nullable();
$table->string('img')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
Story model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Reviews;
use App\User;
use App\Category;
use App\ReadingList;
class Story extends Model
{
protected $guarded = [];
public function readingList()
{
return $this->belongsTo(ReadingList::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function reviews() {
return $this->hasMany(Reviews::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
Category model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Story;
class Category extends Model
{
protected $guarded = [];
public function story()
{
return $this->hasMany(Story::class);
}
}
Been fixated on this for days, maybe you guys can see something I don't. Thank you so much in advance.
You should change date in migration file names
Example
2019_07_29_113341_create_categories_table
2019_07_30_113341_create_stories_table
the older date time would be the first to run,
you should migrate categories table then stories
Hope help you
That's because your categories table migration runs after stories table however stories table depends on categories because of the foreign keys. What you have to do is :
Rename the categories table migration and move it to top of the stories table migration
Drop all tables in the the DB
Run your migration again
you have to consider that first the Parent migration should be created and then the relationship should be established.
Create the "categories" migration first Then create the "stories" migration.
I hope it is useful..
You can create a single migration file in these scenarios.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category');
$table->string('title')->nullable();
$table->string('img')->nullable();
$table->timestamps();
});
Schema::create('stories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('story');
$table->date('published_on');
$table->integer('count_views')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger("category_id");
$table->foreign('category_id')->references('id')->on('categories')->ondDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
Schema::dropIfExists('stories');
}
}

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 come I can't migrate in laravel

Migration files in laravel is used to create the tables in the database, right? But when ever I try to migrate it gives me this error:
C:\xampp\htdocs\app>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (
id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null,
password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at tim
estamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
and I created a new migration file and it's called test. I know users already exist but I want create the new table I created which is called test.
here is the migration file i am going to use to create my table but wont create:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('tests');
}
}
here is the users migration file that tells me it exist:
<?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::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');
}
}
here is the password migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
here is the dummies migration file i have that i didn't talk about because i thought it is not the problem:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDummiesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('dummies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('body');
$table->timestamp('date'); //if you dont put name for the timestamp it will create: create_at and update_at fields.
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('dummies');
}
}
and sorry for keeping you guys waiting it took me long to find the edit button and fix the spacing of my code. It is my first time using stack over flow.
The migrations table in your database is out of sync with the rest of your database. Laravel is trying to re-run a migration to create the users table and failing.
If this is an entirely new project, you could delete all tables from your database and re-run all migrations. Alternatively, fix your migration table so that it accurately reflects the state of your database.
In your laravel/app/providers/AppServiceProvider.php
add the following code inside boot
Schema::defaultStringLength(191);
Then try php artisan migrate:refresh
or best
drop all tables and run php artisan migrate again
Add following code in the start of your up function() of all tables
public function up()
{
// Add this line
Schema::dropIfExists('users');
// from you down method
Schema::create('users', function (Blueprint $table) {
...........
});
}
Try this...
*// AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
function boot()
{
Schema::defaultStringLength(191);
}
*

Laravel 5: php artisan migrate:refresh

I am working on a laravel project and each time I change my table (add or remove column) and run php artisan migrate:refresh. I get this error:
[Symfony\Component\Debug\Exception\FatalErrorException] Can't use
method return value in write context
Solution tried:
run composer dump-autoload (Fails)
Drop table in database, delete the migration file and restart again (works)
Previous migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Changed migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
I added the user_id in the change file in the up function
Try this command it works for me
php artisan migrate:fresh
However, be careful! This command will drop all data from your DB:
Note: The migrate:fresh command will drop all tables from the database and then execute the migrate command.
as per Laravel docs 9.x.
try this
fire this command
php artisan make:migration add_user_id_to_comments_table --table=comments
this will create a new migration file then
$table->integer('user_id')->after('id');
then use
php artisan migrate
Refresh the database and run all database seeds...
php artisan migrate:fresh --seed
Read Docs

Laravel migrations error

When I run php artisan migrate i get error B
Base table or view already exists: 1050 Table 'categories' already
exists'
What is it? Why? How to find error?
My categories migrations file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->index();
$table->text('description');
$table->integer('attachment_id')->unsigned()->index();
$table->foreign('attachment_id')->references('id')->on('attachment')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
In that migration file, the function drop() should be
public function down()
{
Schema::drop('categories');
}
It seems that you try to create existing table..
so i think you are editing the migration file that you already installed
you can drop your database and re-migrate it
php artisan migrate:reset
then
php artisan migrate

Categories