Skip to content

Indexes

Indexes are used to optimize the performance of database queries by allowing the database to quickly locate specific rows. The Schema package provides a simple way to define indexes on columns.

Defining indexes

You can define an index on a column by calling the index() method on the Schema object. The argument is the name of the column to be indexed.

php
$schema = new Schema( 'my_table', function ( Schema $table ) {
	$table->index( 'column_name' );
});

Custom index names

You can also specify a custom name for the index by passing a string as the second argument to the index() method.

php
$schema = new Schema( 'my_table', function ( Schema $table ) {
	$table->index( 'column_name', 'custom_index_name' );
});

Index types

The Schema package supports the following index types:

  • primary(): Defines the column as a primary key, ensuring that each value in the column is unique and not null.
  • unique(): Defines the column as a unique index, ensuring that each value in the column is unique.
  • full_text(): Defines the column as a full-text index, allowing the database to quickly locate specific rows.

Example:

php
$schema = new Schema( 'my_table', function ( Schema $table ) {
	// Define columns here...

	// Define indexes here...
	$table->index( 'column_name' )->primary();
});