Laravel migration files - laravel

I am using php artisan migrate to run the migration file. But it does not creating tables in database. Then I delete "user and password reset" migration files and run the php artisan migrate command. Then my new migration files run successfully and create table in database.
And when I creates more migration files and run the command, again its not creating tables in database. Again to create tables I have deleted the previous made migration files which was created by me.
Please help me with this. Each time I have to delete one or more migration files to run the new migration files ?
And what is this error Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes ?

If your version of MySQL is less than 5.7 you need to set the default string length in your AppServiceProvider.
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
This is outlined in the
Laravel documentation.

Related

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ahimalayan.localizations' doesn't exist

I'm not a Laravel developer, but I bought a website from CodeCanyon, and this is the error I'm getting.
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'ahimalayan.localizations' doesn't exist (SQL: select * from
localizations where ip = 223.185.4 limit 1)
And the service provider is not answering much, and I need to upload it asap but he told me to use php artisan migrate:fresh—seed, but I'm not sure where to use it and how to use it. Can anyone please help? I'm a little bit friendly with coding.
I suggest this steps for your problem :
you need to create a new database in host control panel
Rename example.env file name to .env in main root folder and put new database data inside that. just like this:
DB_DATABASE=DB NAME
DB_USERNAME=DB USERNAME
DB_PASSWORD=DB PASSWORD
then add this line inside routes/web.php file:
Route::get('migrate', function() {
\Illuminate\Support\Facades\Artisan::call('migrate:fresh -—seed');
});
now open this url on your browser to run migrations and seeds. then all required tables will be created automatically and some demo data will be inserted as well.
http://localhost/LaravelFolder/public/migrate
You likely need to run these 2 commands in your app root path.
php artisan migrate
php artisan db:seed

codeigniter 4 migration file not working if having same suffix?

I've just started learning about codeigniter 4. I'm trying to migrate my database but when I try to migrate the same table twice, it gives an error Cannot declare class App\Database\Migrations\Barang, because the name is already in use.
but none of my migration files have the same name as you can see in the image below
Explanation:
When you run php spark make:migration barang, CodeIgniter 4 creates a migration file app/Database/Migrations/2022-03-22-135842_Barang.php having a class name Barang under the namespace App\Database\Migrations.
When you run php spark make:migration barang the second or third time, CodeIgniter 4 performs the same process as specified above except that the respective migration files will have different filenames more specifically different "timestamp prefixes".
When you now run php spark migrate to initiate a migration process, CodeIgniter 4 realizes that you have conflicting class names under the same namespace, hence the error.
Solution:
Always specify unique "migration names" when creating a new migration using the command:
php spark make:migration unique_migration_name_here
Most preferably, the "migration name" should briefly describe the actual intention of the migration. I.e
php spark make:migration alter_users_rename_profile_pic_add_timestamps
Check this answer I posted earlier:
codeigniter 4 migration file not creating table

Artisan rollback, Undefined index (file missing)

I'm trying to do a rollback in Laravel 5.4. I deleted a file manually from the migrations folder. Now when I try to rollback I get an error:
[root#xxx]# php artisan migrate:rollback
[ErrorException]
Undefined index: 2017_08_22_204640_create_tasks_table
That file was a test (with a controller and a view), but I'm new on Laravel and didn't know that deleting that file manually would be a problem.
How can repair that now?
Edit: The main goal is to rename a column from a table made with migrations.
I made this migration file:
[root#xxx]# php artisan make:migration rename_nombre_generadora_column --table="admin_generadora" --create
Created Migration: 2017_08_24_160600_rename_nombre_generadora_column
But then I saw that the name of the table was wrong, should be admin_generadoras. I wanted to create a new migration file but I got this error:
[root#xxx]# php artisan make:migration rename_nombre_generadora_column --table="admin_generadoras" --create
[InvalidArgumentException]
A RenameNombreGeneradoraColumn migration already exists.
So how can I undo that migration?
In situations like this I always tend to simply delete whole database and run "correct" migrations again. You have seeds right? You have factories right?
I copy some text I have written before:
Note for migrations and how to do it right (IMHO):
always use migrations to create and update database schema
if you are a lone developer AND there is no production environment set up, just amend the migrations and do database reset (remove all tables) + migrate
if you are a team OR there is production environment already set up, always create new migration
do not bother with down() method that much
Some material from creator & friends of Laravel regarding migrations can be heard in this podcast http://www.laravelpodcast.com/episodes/68236-episode-53-bigger-better around 30 minute mark.
What you need to do now is to understand deeply how migrations work! To do that you need to take a look at migrations table and play with it; play with artisan and migrations related commands. And remember you can update/amend database schema with SQL (that means using some kind of database client like PhpMyAdmin) but also amend/update corresponding migration.
the --table flag just helps you fill out the migration file faster. Just go in to the RenameNombreGeneradoraColumn file and change admin_generadora to admin_generadoras

Deleted migration files getting executed : Laravel application

I hace deleted some migration files. but when i execute php artisan migrate command, error in the old migration file is showing.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL : alter table cushbu_notifications add constraint
cushbu_notifications_art_id_foreign foreign key (art_id)
references cushbu_arts (id) on de lete cascade)
Now i don't have migration file for cushbu_notifications, instead i created a new migration file for cushbu_user_notifications.I deleted the cushbu_notifications table from database, also the corresponding entry from migrations table.
After manually deleting migration files, clear cache by below command, and then migrate
php artisan cache:clear
then
php artisan migrate
remove the entry from the migration table. and also remove references if that used in another migrations file.
Deleting migration files manually will not remove them from the Laravel cache. You can remove them by executing this.
php artisan optimize

Rollback one specific migration in Laravel

I want
to rollback only :
Rolled back: 2015_05_15_195423_alter_table_web_directories
I run
php artisan migrate:rollback, 3 of my migration are rolling back.
Rolled back: 2015_05_15_195423_alter_table_web_directories
Rolled back: 2015_05_13_135240_create_web_directories_table
Rolled back: 2015_05_13_134411_create_contacts_table
I delete
both of my web_directories and my contacts table unintentionally. I never want that to happen, and if I can rollback only that specific one, this disaster will never happen.
Laravel 5.3+
Rollback one step. Natively.
php artisan migrate:rollback --step=1
And here's the manual page: docs.
Laravel 5.2 and before
No way to do without some hassle. For details, check Martin Bean's answer.
If you look in your migrations table, then you’ll see each migration has a batch number. So when you roll back, it rolls back each migration that was part of the last batch.
If you only want to roll back the very last migration, then just increment the batch number by one. Then next time you run the rollback command, it’ll only roll back that one migration as it’s in a “batch” of its own.
Alternatively, from Laravel 5.3 onwards, you can just run:
php artisan migrate:rollback --step=1
That will rollback the last migration, no matter what its batch number is.
Best way is to create a new migration and make required changes in that.
Worst case workaround (if you have access to DB plus you are okay with a RESET of that table's data):
Go to DB and delete/rename the migration entry for your-specific-migration
Drop the table created by your-specific-migration
Run php artisan migrate --path=/database/migrations/your-specific-migration.php
This will force laravel to run that specific migration as no entry about it exists in Laravel's migration history
UPDATE: The Laravel way (Thanks, #thiago-valente)
Run:
php artisan migrate:rollback --path=/database/migrations/your-specific-migration.php
and then:
php artisan migrate
This will re-run that particular migration
Every time you rollback you get the last batch of migration.
use the command
php artisan migrate:rollback --step=1
better to used refresh migrate
You may rollback & re-migrate a limited number of migrations by
providing the step option to the refresh command. For example, the
following command will rollback & re-migrate the last two migrations:
php artisan migrate:refresh --step=2
otherwise used rollback migrate
You may rollback a limited number of migrations by providing the step
option to the rollback command. For example, the following command
will rollback the last three migrations:
php artisan migrate:rollback --step=3
for more detail about migration see
If you can't do what is told by #Martin Bean, then you can try another trick.
Create a new migration and on that file in up() method insert what's in down() method of the migration you want to rollback and in down() method insert what's in up() method.
e.g if your original migration is like this
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('name');
});
}
public function down()
{
Schema::drop('users');
}
then in new migration file do this
public function up()
{
Schema::drop('users');
}
public function down()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('name');
});
}
and then run the migrate, it will delete the table.
and if you again want that back just rollback it.
Try this:
php artisan migrate:rollback --step=1
This works from Laravel 5.3 +
If you have access to the DB you have a easier solution. You can delete the record from migrations table and then just drop the table. with SQL client.
Or can use
php artisan migrate:rollback --path=...
Like many answers. And remember the path is Location. You can remove even module migration like this. (Any migration from anywhare)
php artisan migrate:rollback --path=Modules/YourModule/database/migrations/2020_05_15_xxxxxx_create_your_table.php
And remember, If you are using linux servers careful about case sensitivity. You have to add like /Database/Migrations with starting capital.
/Database/Migrations/2020_05_15_xxxxxx_create_your_table.php
You can use
php artisan migrate:rollback --path=/database/migrations/timestamp_filename.php
This will remove that specific migration. If the file has foreign key dependency i hoped you use the drop methods. For those cases try creating a new migration and drop foreign if needed.
It might be a little late to answer this question but here's a very good, clean and efficient way to do it I feel. I'll try to be as thorough as possible.
Before creating your migrations create different directories like so:
database
|
migrations
|
batch_1
batch_2
batch_3
Then, when creating your migrations run the following command (using your tables as an example):
php artisan make:migration alter_table_web_directories --path=database/migrations/batch_1
or
php artisan make:migration alter_table_web_directories --path=database/migrations/batch_2
or
php artisan make:migration alter_table_web_directories --path=database/migrations/batch_3
The commands above will make the migration file within the given directory path. Then you can simply run the following command to migrate your files via their assigned directories.
php artisan migrate alter_table_web_directories --path=database/migrations/batch_1
*Note: You can change batch_1 to batch_2 or batch_3 or whatever folder name you're storing the migration files in. As long as it remains within the database/migrations directory or some specified directory.
Next if you need to rollback your specific migrations you can rollback by batch as shown below:
php artisan migrate:rollback --step=1
or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_1
or
php artisan migrate:rollback --step=2
or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_2
or
php artisan migrate:rollback --step=3
or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_3
Using these techniques will allow you more flexibility and control over your database(s) and any modifications made to your schema.
Rollback one step. Natively.
php artisan migrate:rollback --step=1
Rollback two step. Natively.
php artisan migrate:rollback --step=2
If you want to rollback last migration.
php artisan migrate:rollback
If you want to rollback specific migration then go to migration table and set highest value of that record in batch.
Then.
php artisan migrate:rollback
Currently i'm working on laravel 5.8 if not working any other version of laravel please inform to me.
Migrate tables one by one.
Change the batch number of the migration you want to rollback to the highest.
Run migrate:rollback.
May not be the most comfortable way to deal with larger projects.
Use command "php artisan migrate:rollback --step=1" to rollback migration to 1 step back.
For more info check the link :- https://laravel.com/docs/master/migrations#running-migrations
1.) Inside the database, head to the migrations table and delete the entry of the migration related to the table you want to drop.
Migration table image example
2.) Next, delete the table related to the migration you just deleted from instruction 1.
Delete table image example
3.) Finally, do the changes you want to the migration file of the table you deleted from instruction no. 2 then run php artisan migrate to migrate the table again.
If you want modify original migration file and migrate it again, you can use this package to migrate. (Applicable to Laravel 5.4 or later)
First, install package in your Laravel project:
composer require caloskao/migrate-specific
Register command at app/Console/Kernel.php :
protected $commands = [
\CalosKao\MigrateSpecific::class
];
Now, run this command to migrate your file
php artisan migrate:specific database/migrations/table.php
php artisan migrate:rollback --path=/database/migrations/0000_00_00_0000_create_something_table.php
INSERT INTO homestead.bb_migrations (`migration`, `batch`) VALUES ('2016_01_21_064436_create_victory_point_balance_table', '2')
something like this
As stated in the Laravel manual, you may roll back specific number of migrations using the --step option
php artisan migrate:rollback --step=5
Another alternative to those mentioned if you need to do this a bunch of times with the same migration(s). Personally I think this adds a lot of flexibility to your migrations.
Add database/migrations to your autoload object in your composer.json like this:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories",
"database/support",
"database/migrations" // add this line
]
},
Then add namespace Database\Migrations; to all of your migration files.
Then run $ composer dump-autoload to refresh your composer.lock file.
Then, assuming your class name for the migration is AlterTableWebDirectories, you can create a command like this:
$ php artisan make:command DropAlterTableWebDirectories
And write this logic in your handle() method:
public function handle {
(new AlterTableWebDirectories)->down();
DB::raw("delete from migrations where migration like '%alter_table_web_directories%'");
}
This will do exactly what you want. If you want to decrement the migration count instead of deleting it, you can probably figure out how to change the DB:raw command.
This command could be extended to allow you to dynamically choose which migration you're dropping it by passing an argument into the command.
Then when you're reading to migrate that file again, you can just run php artisan migrate and it will only migrate that one.
This process allows you to make specific changes to migrations without having to do a full refresh and seed each time.
Personally I need to do that a lot because my seeds are rather large.
Beyond the aforementioned
php artisan migrate:rollback --step=1
I would add a check to the table migrations:
select * from migrations;
This will show you a ordered list with the migrations that the artisan command is rollingback.
It's quite easy to roll back just a specific migration.
Since the command php artisan migrate:rollback, undo the last database migration,
and the order of the migrations execution is stored in the batch field in the migrations table.
You can edit the batch value of the migration that you want to rollback and set it as the higher.
Then you can rollback that migration with a simple:
php artisan migrate:rollback
After editing the same migration you can execute it again with a simple
php artisan migrate
NOTICE: if two or more migrations have the same higher value, they will be all rolled back at the same time.
To roll back for only one table you should use path option and add migration table path with step one option in command line:
php artisan migrate:rollback --step=1 --path=database/migrations/2022_12_12_050004__alter_table_web_directories.php
Rolling back a specific migration in Laravel is a straightforward process.
Here are the steps to follow:
First, open your terminal or command prompt and navigate to your Laravel project directory.
Type in the command php artisan migrate:status to view the status of all the migrations in your project.
Identify the migration you want to rollback, and copy its migration name (e.g., 2022_01_01_000000_create_users_table).
Type in the command php artisan migrate:rollback --step=1 --pretend --path=/database/migrations/2022_01_01_000000_create_users_table.php.
Replace the migration name with the name of the migration you want to rollback.
Hit enter, and Laravel will rollback the specified migration.

Resources