[7.x] Add the ability to create indexes as expressions #32411
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the ability to create indexes as expressions. Previously (to the best of my knowledge), the only way to do this was using
DB::statement()
. For example:Not only does this look awful, it also doesn't take advantage of any platform specific grammar (should the expressions themselves be cross platform, of course).
This PR adds a new
rawIndex()
method to theBlueprint
class, which allows for a much more Laravel-style syntax for creating expression indexes. For example:The other method name I considered was
$table->expressionIndex()
, but I felt this was probably more appropriate, since it's essentially using a raw expression under the hood.This method requires an index name as the second argument, since there isn't a nice way to autogenerate the name.
I've tested this on MySQL, Postgres, and SQLite, and it works great. I haven't tested on it on SQL Server (no access to it), but I'm pretty confident it will work.
Compatibility: