Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add column index in Laravel?

I have tried this in my migration but it does not work as expected.

$table->string('key')->primary()->index();

I also tried to use chatgpt but no solutions were correct. So I hit brick wall debugging, I know its something obvious I am missing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Welcome to SO, its been answered before but the simples way to index your column is to add this in a new migration, so firstly create a migration like

php artisan make:migration add_index_to_key_column_on_table

and in that migration

Schema::table('table', function (Blueprint $table) {
    $table->index(['key']);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading