Skip to content

Latest commit

 

History

History
99 lines (80 loc) · 2.84 KB

indexes.md

File metadata and controls

99 lines (80 loc) · 2.84 KB

Schema Management

Indexes

💡️ Tip: If you are creating an index on a single column, you can use the Column Index Shorthand syntax on the Column.

Available Properties

return [
    'users' => [
        'columns' => [
            ...
        ],
        'indexes' => [
            'users' => [
                'type' => IndexType::unique,
                'indexColumns' => [
                    'team_id',
                    'email',
                ],
            ],
        ],
        ],
    ],
];
Property Description Type
<array key> Name of the index string
type Type of the index (see below) IndexType enum
indexColumns Array of column names for the index array

Index Types

The following index types are supported in the IndexType enum.

  • IndexType::index
  • IndexType::fullText
  • IndexType::primary
  • IndexType::spatialIndex (⚠️ untested)
  • IndexType::unique

For more details, see the Laravel documentation on index types.

Adding an index

💡If your index is on a single column, you can use the Column Index Shorthand syntax on the column definition.

To create a new index, add a new entry to the indexes array in the schema definition.

return [
    'users' => [
        'columns' => [
           ...
        ],
        'indexes' => [
+           'users' => [
+               'type' => IndexType::unique,
+               'indexColumns' => [
+                   'team_id',
+                   'email',
+               ],
+           ],
        ],
    ],
];

Then run the Diff command to generate the migrations.

‼️ When adding a new index, make sure to exclude the realoquentId property. Realoquent will add this automatically.

Removing an index

To delete an index, simply remove the array key from the schema.php file and run the Diff command.

Renaming an index

To change an index name, simply change the array key in the schema.php file and run the Diff command.

'indexes' => [
-   'users' => [
+   'users_unqiue_team_email' => [
        'type' => IndexType::unique,
        'indexColumns' => [
            'team_id',
            'email',
        ],
    ],
],

See Laravel documentation for which databases support index renames.