diff --git a/database.md b/database.md index 82abc1982d..c86488477e 100644 --- a/database.md +++ b/database.md @@ -102,8 +102,8 @@ To see how read / write connections should be configured, let's look at this exa 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), - 'charset' => 'utf8mb4', - 'collation' => 'utf8mb4_0900_ai_ci', + 'charset' => env('DB_CHARSET', 'utf8mb4'), + 'collation' => env('DB_COLLATION', 'utf8mb4_0900_ai_ci'), 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, @@ -412,6 +412,20 @@ If you would like to include table row counts and database view details within t php artisan db:show --counts --views ``` +In addition, you may use the following `Schema` methods to inspect your database: + + use Illuminate\Support\Facades\Schema; + + $tables = Schema::getTables(); + $views = Schema::getViews(); + $columns = Schema::getColumns('users'); + $indexes = Schema::getIndexes('users'); + $foreignKeys = Schema::getForeignKeys('users'); + +If you would like to inspect a database connection that is not your application's default connection, you may use the `connection` method: + + $columns = Schema::connection('sqlite')->getColumns('users'); + #### Table Overview diff --git a/migrations.md b/migrations.md index 9df9c3a420..6df04a5d48 100644 --- a/migrations.md +++ b/migrations.md @@ -267,7 +267,7 @@ When creating the table, you may use any of the schema builder's [column methods #### Determining Table / Column Existence -You may determine the existence of a table or column using the `hasTable` and `hasColumn` methods: +You may determine the existence of a table, column, or index using the `hasTable`, `hasColumn`, and `hasIndex` methods: if (Schema::hasTable('users')) { // The "users" table exists... @@ -277,6 +277,10 @@ You may determine the existence of a table or column using the `hasTable` and `h // The "users" table exists and has an "email" column... } + if (Schema::hasIndex('users', ['email'], 'unique')) { + // The "users" table exists and has a unique index on the "email" column... + } + #### Database Connection and Table Options @@ -289,7 +293,7 @@ If you want to perform a schema operation on a database connection that is not y In addition, a few other properties and methods may be used to define other aspects of the table's creation. The `engine` property may be used to specify the table's storage engine when using MySQL: Schema::create('users', function (Blueprint $table) { - $table->engine = 'InnoDB'; + $table->engine('InnoDB'); // ... }); @@ -297,8 +301,8 @@ In addition, a few other properties and methods may be used to define other aspe The `charset` and `collation` properties may be used to specify the character set and collation for the created table when using MySQL: Schema::create('users', function (Blueprint $table) { - $table->charset = 'utf8mb4'; - $table->collation = 'utf8mb4_unicode_ci'; + $table->charset('utf8mb4'); + $table->collation('utf8mb4_unicode_ci'); // ... }); @@ -941,7 +945,7 @@ Modifier | Description `->autoIncrement()` | Set INTEGER columns as auto-incrementing (primary key). `->charset('utf8mb4')` | Specify a character set for the column (MySQL). `->collation('utf8mb4_unicode_ci')` | Specify a collation for the column. -`->comment('my comment')` | Add a comment to a column (MySQL/PostgreSQL). +`->comment('my comment')` | Add a comment to a column (MySQL / PostgreSQL). `->default($value)` | Specify a "default" value for the column. `->first()` | Place the column "first" in the table (MySQL). `->from($integer)` | Set the starting value of an auto-incrementing field (MySQL / PostgreSQL). @@ -951,7 +955,7 @@ Modifier | Description `->unsigned()` | Set INTEGER columns as UNSIGNED (MySQL). `->useCurrent()` | Set TIMESTAMP columns to use CURRENT_TIMESTAMP as default value. `->useCurrentOnUpdate()` | Set TIMESTAMP columns to use CURRENT_TIMESTAMP when a record is updated (MySQL). -`->virtualAs($expression)` | Create a virtual generated column (MySQL / PostgreSQL / SQLite). +`->virtualAs($expression)` | Create a virtual generated column (MySQL / SQLite). `->generatedAs($expression)` | Create an identity column with specified sequence options (PostgreSQL). `->always()` | Defines the precedence of sequence values over input for an identity column (PostgreSQL). @@ -1097,7 +1101,7 @@ Command | Description `$table->primary(['id', 'parent_id']);` | Adds composite keys. `$table->unique('email');` | Adds a unique index. `$table->index('state');` | Adds an index. -`$table->fullText('body');` | Adds a full text index (MySQL/PostgreSQL). +`$table->fullText('body');` | Adds a full text index (MySQL / PostgreSQL). `$table->fullText('body')->language('english');` | Adds a full text index of the specified language (PostgreSQL). `$table->spatialIndex('location');` | Adds a spatial index (except SQLite). diff --git a/releases.md b/releases.md index ba8d2c2ed9..ec46860f3b 100644 --- a/releases.md +++ b/releases.md @@ -351,3 +351,18 @@ Laravel 11 includes improved support for MariaDB. In previous Laravel releases, For more information on Laravel's database drivers, check out the [database documentation](/docs/{{version}}/database). + +### Inspecting Databases and Improved Schema Operations + +_Improved schema operations and database inspection was contributed by [Hafez Divandari](https://github.com/hafezdivandari)_ + +Laravel 11 provides additional database schema operation and inspection methods, including the native modifying, renaming, and dropping of columns. Furthermore, advanced spatial types, non-default schema names, and native schema methods for manipulating tables, views, columns, indexes, and foreign keys are provided: + + use Illuminate\Support\Facades\Schema; + + $tables = Schema::getTables(); + $views = Schema::getViews(); + $columns = Schema::getColumns('users'); + $indexes = Schema::getIndexes('users'); + $foreignKeys = Schema::getForeignKeys('users'); +