22

I had a good working Laravel structure with database migrations and seeding. But I wanted my migration file,classes,db table be renamed to CreateOrganizationsTable instead of CreateOrganisationsTable

So I changed the migration filename, all classes and routes.

But when I execute php artisan migrate:reset in my Homestead box, I get the following error:

PHP Fatal error: Class 'CreateOrganisationsTable' not found in /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 299 

Artisan created some link I'm unable to fix at the moment.

7
  • Can you rename it back to make it work? Commented Aug 4, 2014 at 9:03
  • 14
    have you done a composer dump-autoload? Commented Aug 4, 2014 at 9:06
  • 1
    I got it back to work again by executing composer dump-autoload immediately after renaming the migration class. If you execute any php artisan migrate command before the composer command, the composer dump-autoload doesn't have any effect. So the command order is crucial. Thanks Mark Commented Aug 4, 2014 at 9:26
  • 2
    Have you also tried running: php artisan dump-autoload? Commented Aug 4, 2014 at 9:37
  • 3
    composer dump-autoload after renaming the file and the column in the migrations table worked for me! Commented Feb 10, 2015 at 2:23

10 Answers 10

35

As Morale mentioned you must reset or rollback to a point before your new migration or make the changes manually. I knew this but was still getting the problem after changing only the timestamps to have the migrations run in a different order.

You must run composer dump-autoload even if you didn't change the class name.

Sign up to request clarification or add additional context in comments.

Comments

13

Don't forget to also rename the class inside the file

1 Comment

Just to elaborate on this, class CreateResourceTagsTable extends Migration would need to become class CreateTagsTable extends Migration if I were to rename my migration to create_tags_table.
6

I have had similar problems with the php artisan:rollback, and I just opened my laravel project and went to the next directory:

vendor\composer\autoload_static.php

and modified the name of the php migration that was changed, hope this help's you :)

Comments

6
  1. rename your file
  2. rename the class
  3. rename the 'migration' column in 'migrations' table in database.

Comments

5

The problem is that when you run the migrate:reset command, artisan wants to call the down method on all classes. And since you renamed the class, he can't find it anymore!

So before renaming your class and/or file, run php artisan migrate:reset which will clean your database and remove all tables, and right after that simply run php artisan migrate. You should have a working database again.

If the migrate:reset command still doesn't work, you can simply delete the tables manually (don't forget to remove the migration table aswell) and run php artisan migrate again.

Edit: In case you don't actually want to run the migrate:reset, but just rename the migration, you can edit the name of the file (and class) . But after that you have to manually edit the table migrations in your database. Find the corresponding row, and edit the name of the file to match the new name.

Comments

3

In laravel renaming migration works fine if you do it the right way.

i have also renamed my migration files many times, and it works as expected,

but if you want to rename the migration then you have to take care of two things:

  1. first rename the filename

  2. open that migration file and rename the Class name also

Renaming the file : change your filename from 2019_06_28_131130_create_organisations_table to 2019_06_28_131130_create_organizations_table or whatever name you want.

Open that migration file and Rename the class name in that file as per your new name:

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; //change it from //class CreateOrganisationsTable extends Migration { //to class CreateOrganizationsTable extends Migration { 

i think you have renamed the migration file but you have forgotten to rename the Class name so do that and try to run php artisan migrate:refresh it will work as you want.

Comments

2

I've performed a composer dump-autoload and everything works fine now

Comments

1

If you're using composer, then I think you forgot to dump-autoload

composer dump-autoload 

Comments

0

If all the above fail, as they did for me try setting directly the name of the table

class NameModel extends Model { public $table = 'name_exact_of_the_table'; 

Answer via How to fix error Base table or view not found: 1146 Table laravel relationship table? credit https://stackoverflow.com/users/1607975/jpasosa

Comments

0

The solutions proposed above did not suit me because they involved dropping tables, which is not desirable in production.

Here is my solution :

Laravel has created a table which records the name of migration files that have already been run, which can be displayed with :

$migrations = DB::table('migrations')->get(); dd($migrations); 

From there you can modify the name recorded by your application of your migration file :

DB::table('migrations')->where('migration', $old_file_name) ->update([ 'migration' => $new_file_name, ]); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.