Just a quick one really
I have an existing Laravel app
I want to use a migration to add some columns to an existing table
So i do the following:
php artisan make:migration add_columns_to_table
Which creates a file in my migrations folder called:
2017_10_25_124938_add_column_to_table.php
And also creates an entry in my migrations table in the database. All good.
I can edit the migration file (adding the columns etc) and run
php artisan migrate
And everything works great
Now - my question is this:
When i come to deploy to live i am presuming I would log onto the live box and run the create migration command again:
php artisan make:migration add_columns_to_table
But this will create a migration with a different name to the one i created / tested locally?
So - do i then need to manually copy the code from my local 2017_10_25_124938_add_column_to_table.php migration file to the one created on the live box?
That seems a bit backwards and fiddly
What is the best way of creating and testing a migration locally and then deploying it to live when the create migration command creates a different named migration file (and DB entry) on the live box?
Or have i got the wrong end of the stick?
The best practice is to keep migration files with the repo. While deploying the application on any environment you have to execute all migrations.
php artisan migrate
This command will run all the migrations which are not already executed in the current environment. Laravel use a table called 'migrations' to keep track of all the migrations it has run.
Related
I have a migration file in a Laravel project that sits on a slightly-older branch. In newer branches, newer migrations have been created and ran in production. I have ran the migration in my local environment, but in no other environment.
What will happen if I need to push this migration to the master branch, then to production? My assumption is that it will just not run since a newer migration has run, but that is an assumption.
What is the best way to handle something like this? I could not find anything clear in the laravel documentation for Laravel 5.6.
My assumption is that it will just not run since a newer migration has run, but that is an assumption.
Laravel tracks which migrations it has run in the database's migrations table. If a migration is in the directory and hasn't been run before, it will get applied next time artisan migrate is run, regardless of the fact that "newer" migrations have already been run. The migration code is not inherently aware of dates; it's running the migrations in filename order, as you can see in the relevant code.
I have an issue with laravel.
I have created a database named billing and connection is working fine for all tables. Now I created a new table named as masters. When I'm using this table then it is showing error base table or view masters not found
How can I resolve this issue?
This is usual for beginners. I have a few hints:
You might want to manually check the database, outside of Laravel, to
be sure the table has not been add.
Sometimes, you might just have misspelt the table name.
Did you forget to run the migration? If so, you need to run the
migration to add the table to the database. You can simple use php
artisan migrate on the command line.
If all these don't resolve it, dump you autoload files with composer and rerun your migration. You can use composer dumpautoload and then php artisan migrate:refresh
To create a migration, use the make:migration Artisan command:
php artisan make:migration create_masters_table
The new migration will be placed in your database/migrations directory.
You can use Laravel schema builder to expressively create and modify tables.
To run all of your outstanding migrations, execute the migrate Artisan command: php artisan migrate
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 having a very weird issue when running
php artisan migrate
on a new installation of Laravel 5.4 on a local XAMPP server with php 7.1.1.
What it is doing is creating a migrations and users table, the default tables in older versions of Laravel. What is should be creating is a users table and a passwords reset table, the new default tables. My env file is correct because I am connecting to the database correctly but then if I change the table I am connecting to in the env file it is not updating.
To me, it seems like the command is simply not running on the correct migrations folder in the application. This is a brand new machine with a fresh install of XAMPP and Laravel so I am very confused as to why this is happening. Any help would be much appreciated!!!
I was finally able to get this to work so I wanted to answer the question to explain what I did.
This first step may not be required for you but it was for me. Once again, here is what I was using: Laravel 5.4 on a local XAMPP server with php 7.1.1
-First I needed to edit app>Providers>AppServiceProvider.php https://laravel-news.com/laravel-5-4-key-too-long-error
-Add this line to the top, under the other use include: use Illuminate\Support\Facades\Schema;
-In the boot() function add this code: Schema::defaultStringLength(191);
-After that I needed to run php artisan migrate, to create the default tables from the migrations. For some reason, a reason I could not find with any amount of research, this creates the old tables from an unknown source. After that I needed to edit the create_users_table migration, delete the users and migrations table that were created in the database, then rerun php artisan migrate. Like I said, I am not sure why this has to be done but it will get it to work.
Also, remember to run these commands before running php artisan migrate the last time just to be safe:
php artisan cache:clear
php artisan config:cache
php artisan migrate:refresh
php artisan migrate
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