I have been working using laravel for a long time. I was thinking about this problem. Is it possible to drop a specific table and remove migration file using just a artisan command?
I know there are couple of ways to do these job like Schema::drop('') or creating new migration etc. I am just curious to find such artisan command to do these job.
Any idea will be appreciated.
Thanks.
It's not possible to do this in one command without writing your own artisan command (artisan make:command) - https://laravel.com/docs/5.3/artisan#writing-commands
You can create your custom artisan command.
artisan make:command
Related
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
after I've manually deleted a table in mysql, I run the command:
php artisan migrate
and I get this:
Nothing to migrate.
it only works the first time, how to re-run the migration in laravel?
Laravel keeps a record of migrations that have been run. You need to go to the migrations table and delete the migration record. Laravel does this so that it knows which migrations have been run so that it doesn't run them again and so that in the case of a rollback, it knows the last batch of migrations which were done.
This may help https://laravel.com/docs/5.6/migrations
Try composer dump-autoload AND php artisan config:cache
if not working also Try php artisan migrate:refresh.
OR Also Delete Migration table in database.
You can simply do: php artisan migrate: fresh to re-migrate your migration and if you have seeder data then you simply do: php artisan migrate: fresh -- seed
If you delete a table by hand, will have to delete the record from "migration" table too. This way when artisan migrate checks if your migration are not in "migration" table will migrate the unregistered ones.
That is the expected behaviour if you manually delete a table, because the previous batch migration job has already been deployed (in migrations table).
IF you want to re-migrate all the database, you can simply do: php artisan migrate:refresh.
IF you want to make sure your database to be clean with your latest changes, you can drop your entire database tables and do php artisan migrate again. Also, you can try php artisan migrate --seed if you have any seeder.
The best thing you can do is drop the table from the database and then rename the migration last number and do
php artisan migrate
This doesn't recreate all the tables.
I have a lot of Migrations but I just want to run one.
I tried it many times, but every time all the Migrations were run.
Try this
php artisan migrate --path=/database/migrations/selected/
put migrations in more folders and run
php artisan migrate --path=/app/database/migrations/new_migrations
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
What's prefered (or best practice for migration), run artisan migration everytime we created a new migration file, or after we created some?
Everytime:
Create migration file
Run php artisan migrate
Create another migration file
Run php artisan migrate
Create another migration file
Run php artisan migrate
etc
After created some:
Create migration file
Create another migration file
Create another migration file
Run php artisan migrate
etc
There is no preferred way of creating migrations, it's as you like and depends on your project.