How to seed database migrations for laravel tests?

All you need to do is make an artisan call db:seed in the setUp function <?php use Illuminate\Foundation\Testing\DatabaseMigrations; class ExampleTest extends TestCase { use DatabaseMigrations; public function setUp(): void { parent::setUp(); // seed the database $this->artisan(‘db:seed’); // alternatively you can call // $this->seed(); } /** * A basic functional test example. * * @return void … Read more

Drop Unique Index Laravel

By official documentation You can see following: If you pass an array of columns into a method that drops indexes, the conventional index name will be generated based on the table name, columns and key type: Schema::table(‘geo’, function ($table) { $table->dropIndex([‘state’]); // Drops index ‘geo_state_index’ }); You can drop it just simply using [] around … Read more

naming tables in many to many relationships laravel

Laravel’s naming convention for pivot tables is snake_cased model names in alphabetical order separated by an underscore. So, if one model is Feature, and the other model is Product, the pivot table will be feature_product. You are free to use any table name you want (such as product_feature), but you will then need to specify … Read more

How to drop softDeletes from a table in a migration

On your migration class use dropSoftDeletes(): public function down() { Schema::table(“users”, function ($table) { $table->dropSoftDeletes(); }); } This method can be found in Illuminate\Database\Schema\Blueprint.php: public function dropSoftDeletes() { $this->dropColumn(‘deleted_at’); } Since Laravel 5.5, this information can be found in the documentation.

Artisan, creating tables in database

Migration files must match the pattern *_*.php, or else they won’t be found. Since users.php does not match this pattern (it has no underscore), this file will not be found by the migrator. Ideally, you should be creating your migration files using artisan: php artisan make:migration create_users_table This will create the file with the appropriate … Read more

How to convert Laravel migrations to raw SQL scripts?

Use the migrate command You can add the –pretend flag when you run php artisan migrate to output the queries to the terminal: php artisan migrate –pretend This will look something like this: Migration table created successfully. CreateUsersTable: create table “users” (“id” integer not null primary key autoincrement, “name” varchar not null, “email” varchar not … Read more

Add sql table column before or after specific other column – by migrations in Laravel 4.1

Yes. Create a new migration using php artisan migrate:make update_users_table. Then use the table command as follows (if you’re using MySQL!): Schema::table(‘users’, function($table) { $table->string(‘phone_nr’)->after(‘id’); }); Once you’ve finished your migration, save it and run it using php artisan migrate and your table will be updated. Documentation: https://laravel.com/docs/4.2/migrations#column-modifiers

Error migrations: Cannot declare class X, because the name is already in use

As well as other answers given, this error can also happen if the migration filename is not a snake-case version of the class name. So a migration file 2019_01_18_020910_create_roles_table.php must contain the class CreateRolesTable. If it contains the class CreateRoleTable, with a missing s, then the “Cannot declare X…” error is thrown. I’ve found this … Read more