Trying to check if a unique index exists on a table when preparing a migration, how can it be achieved?
Schema::table('persons', function (Blueprint $table) { if ($table->hasIndex('persons_body_unique')) { $table->dropUnique('persons_body_unique'); } })
Something that looks like the above. (apparently, hasIndex() doesn't exist)
It's the way to say to the Laravel Migration to add indices to that column, in order to get faster results when searching through that particular column. It's a common procedure in DB design when building tables. Just "index" some particular columns if you plan to make searchs in the table using those columns.
Migration is the process by which Content Manager OnDemand moves index data from the database to archive storage. Migration optimizes database storage space while allowing you to maintain index data for a very long time.
Each migration file name contains a timestamp, which allows Laravel to determine the order of the migrations. If you would like to specify a custom output path for the generated migration, you may use the --path option when executing the make:migration command.
To see the index for a specific table use SHOW INDEX: SHOW INDEX FROM yourtable; To see indexes for all tables within a specific schema you can use the STATISTICS table from INFORMATION_SCHEMA: SELECT DISTINCT TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.
Using "doctrine-dbal" that Laravel uses is better solution:
Schema::table('persons', function (Blueprint $table) { $sm = Schema::getConnection()->getDoctrineSchemaManager(); $indexesFound = $sm->listTableIndexes('persons'); if(array_key_exists("persons_body_unique", $indexesFound)) $table->dropUnique("persons_body_unique"); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With