Laravel 4 migrate rollback problems
PhpLaravelPhp Problem Overview
I can easily run the artisan migrate etc, but when i try to roll it back, with migration:rollback i keep getting this error,
c:\xampp\htdocs\laravel>php artisan migrate:rollback
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'CreateCodesnippetsTable' not found","file":"C:\\xampp\\htdocs\\laravel\\vendor\\laravel\\framework\\src\\Illum
inate\\Database\\Migrations\\Migrator.php","line":301}}
Is this a bug? or how should i debug this?
Php Solutions
Solution 1 - Php
Maybe you've already solved this issue. But I'm noticing that for some reason a rollback often requires you to run composer dumpautoload
first. Even if your migration works.
Solution 2 - Php
Having just wrestled with this problem for several days, I think I can now provide the definitive answer to solving this problem. Yeah, big call I know, but bear with me.
The first port of call if you encounter this problem is to run composer dump-autoload
. This should result in an updated version of the file vendor/composer/autoload_classmap.php
.
If autoload_classmap.php
doesn't get updated then you may have a permissions problem, in which case you could try sudo composer dump-autoload
.
However, if autoload_classmap.php
does get updated, check that it contains an entry for your migration class (in this case CreateCodesnippetsTable
). If there is no entry for this class then you should check your composer.json
file and make sure the app/database/migrations
folder is included in the autoload section, eg:
"autoload": {
"classmap": [
"app/controllers",
"app/models",
"app/database/migrations"
]
},
This last bit is what screwed things up for me. In a misguided attempt at optimising things I pulled as much as I could out of my composer.json
file, naively thinking this would only affect web requests. It turns out this affected Artisan as well, so putting this line back in and running composer dump-autoload
fixed the problem for me.
Finally, if all that fails, then maybe there's a bug in one of the supporting libraries that is causing the problem, in which case you can try updating using composer update
or some variation thereof. However, I suspect that this will rarely be the true cause of the problem.
Solution 3 - Php
If you are in windows, simply use composer in your terminal/command line utility and do the following:
composer dump-autoload
Hope it helps!
Solution 4 - Php
From what I can see I am guessing you have changed the class name manually.
In the error you have the class name CreateCodesnippetsTable
but in the migration file you provided (pastebin), the class name is CreateCodeSnippetsTable
(notice the S in Snippets, I guess that is what you changed manually).
If you check the migrations table in your database you will see records for each migration. When you create the migration it will be saved in the database with that name and the rollback method tries to read the file with the name provided in the database, in the case when you change it manually laravel can't find the class and you get the error.
To fix this you can undo the changes and try to rollback or manually edit the migration row in your database to include the correct class name.
Hope this helps.
Solution 5 - Php
It seems to me there are no single solution of this error. I have tried many suggestion but at last this one works in my end.
COMPOSER=composer.json composer dump-autoload
Solution 6 - Php
I fixed it by running
composer.phar update
Solution 7 - Php
download the composer.phar file from laravel site and bring the composer.phar file to the root directory of laravel folder,
then from terminal come to the root directory of laravel and run the composer.phar update
or simply run php artisan dump-autoload
.
Solution 8 - Php
i faced the same problem and figure out the problem
I've created a migration for adding new column date in PatientReasonOfVisits table i used laravel generators when i create the migration the class name was
class AddDateToPatientReasonOfVisitsTable
sure after creating a new migration file u need to run composer dump-autoload to ensure the file is listed in class map file
the file name was 2014_09_02_214134_add_date_to_patientreasonofvisitstable.php
the migration done successfully and a new record has been added into migration table. in migration column the file name is used
when i rollback the migration i got the class not found exception what class is not found this one
AddDateToPatientreasonofvisitsTable
note : the difference between classes names
why and how i solved this problem i guess when u rollback the class name resolved using the migration file name which in migration table the capital and small letters is decided by underscores "_" in file name
so after renaming the migration file to 2014_09_02_214134_add_date_to_patient_reason_of_visits_table.php and sure run composer dump-autoload after renaming the file the class name resolved correctly with no exception
Solution 9 - Php
I simply dropped the migrations table then ran php artisan migrate:refresh
Then the migrations were all able to execute, not sure if that's the best method, but it worked for me.
I'm running Laravel 5.