I'm using Laravel 5.8 and each time I create a new migration I run php artisan migrate:refresh to update my database. I lose data stored in tables that already exist. I want to find a solution to add a new table without losing my data.
You can run php artisan migrate (without :refresh), this only runs migrations that have not been run yet.
The idea with migrations is that you do not edit them after you run them.
More info on migrations can be found in the migration documentation
You can use the seeding too if you need some base data when you make a migration:refresh, please see the link to know more about that.
Related
Im new to laravel, someone asked me to modify in his project, these modifications need to have new tables, I created tables directly on phpmyadmin, these tables include countries and regions tables which have huge rows, I wanted to push the project to the server but don't have access to the database so must using migrate, my questions are:
how can I migrate the tables I created in phpmyadmin? I didnt create tables using the command artisan migrate.
how can I push countries and regions tables contents?
thank you in advance
Firstly familarise yourself with the documentation on Laravel Migrations (https://laravel.com/docs/6.x/migrations). The documenation is excellent!
If you want to replicate the structure of the table you have already created, I would start by getting the table schema from phpmyadmin and then simply walking through it line by line and finding the appropriate methods in the documentation.
Once you have matched the table structure, it then just a case of taking a database dump of your existing table and importing it.
To update the production server database you still can use migrations, even if you created tables manually on local environment.
Please read the docs for more info.
Run the command: php artisan make:migration table-name in local environment, and write required columns in created file.
After that, you can upload this file on the server and just run: php artisan migrate this will create tables on the prod database.
To automatically insert data after migrations you can use seeding: https://laravel.com/docs/5.7/seeding
This is not a correct approach, If you are using Laravel and manually uploading the data.
The better solution is to write the migrations and seeder.
You can print db result in an array and paste everything in your Seeder file, Check the syntax on Laravel website
So, I've copied a database off a server that I'm working on and I've just started working on it. However for some reason the migrations table is empty so when I try to run php artisan migrate it can't because it doesn't know that all the tables are already created. Is there a quick way of fixing this rather then adding in the name of every migration to the table manually as there are a lot of migrations in this project?
Thanks.
You can work around it the following way:
Create another empty database.
Switch your application to use the new database.
Run php artisan migrate
Copy migrations table from new database to the old one
Switch your application to use the old database again.
You could also delete existing migration files (bad idea) or move them to some other folder so that migrate command doesn't see them.
You could also fill the table yourself, but that seems pretty time consuming.
Why is that table empty in the first place? Is it empty on your server as well?
I am just beginning to learn laravel and so I am constantly dealing with migration methods right now. Previously, I have deleted a migration file ShippedViaToPurchaseOrders and after that I cannot perform php artisan migrate:reset/rollback anymore. What is the problem with this? Please help. Please take a look with the error below. Thank you so much.
These files are loaded using composer, it still thinks the file is there.
Try running the composer dump-autoload command before running migrations to recreate the autoload file and let composer know the file is no longer there.
Update:
The Laravel migration tool creates a table migrations in your database to know what migrations have been executed. In this table, remove the row corresponding to the removed migration.
Because the row is still there, Laravel will keep trying to run the rollback migration corresponding to that row.
So I just started playing around with Laravel 5.1. I created a model with migration named "teachers". Then I manually deleted the model file which was Teachers.php which contained the Teachers class. Now when I try to roll back I get the following error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'CreateTeachersTable' not found
I tried composer dump-autoload and php artisan migrate:refresh and php artisan migrate:reset but it keeps on giving above error. I just want to start afresh. How to reset everything?
Ok. So I found the solution. So if you accidentally delete any model file or migrations file without using php artisan migrate:rollback the above command and any migrate command will cause problems. SO what you need to do is, delete all the tables manually from the database. I was using sqlite database and I deleted migrations, users, questions, teachers, password_resets all the tables that were generated by migrate command. Then I ran composer dumpautoload. Then I ran php artisan migrate command again after making changes to the files and everything was back to normal.
So basically deleting migrations table solves the issue.
When you refresh your migrations, Laravel looks at your migrations table and uses the values in there to load the migrations files to rollback.
Since you manually deleted the migration file, it was never removed from your migrations table. You'll have to manually delete the row in that table for the given migration file.
First, check if that class is still referenced somewhere by doing a full search within your project folder, and then if it is not, try running:
php artisan clear-compiled
I have 38 migration script that I wrote in Laravel 4.
I don't want to throw them away, but I also don't want to run them either. I just want to keep them as references.
If I place them in the migration folder in Laravel, it will run when I do
php artisan migrate and that will break some part of my database, as they have already been run.
I'm wondering if there is a way to mark them as already run, so Laravel will not trying to run them again.
I notice the migration table in my database - can I do something with it ?
What is the best way I should do to those 38 migrations ? Feel free to give me any suggestions.
Your question is a little confusing -- Laravel will only run each migration once. It keeps track of which migrations have run in the migrations table. The whole idea of migrations is a series of date sortable scripts that you can run at anytime start to finish and they rebuild your database, AND that you can add to without needing to rerun them all as they work (so your data is preserved)
If you're running
php artisan migrate
and Laravel's running a migration it has already run, something is very broken with your system.
(Speculation follows) It seems more likely the latest migration you're running may have halted half way though in a place MySQL couldn't rollback the changes, and Laravel's trying to rerun the latest one. Try a
php artisan migrate:rollback
Fix the error in the breaking migration, and you'll be all set.