From 2eb656bd158830a7a64feeb0c1cb60c6d3de2bc0 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 11 Mar 2024 17:56:34 +0330 Subject: [PATCH 1/2] inspecting database --- database.md | 22 ++++++++++++++++++++-- migrations.md | 18 +++++++++++------- releases.md | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/database.md b/database.md index 82abc1982d4..48c2c46e2b7 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, @@ -394,6 +394,24 @@ php artisan db mysql ## Inspecting Your Databases +You may use following schema methods to inspect your database and its associated tables and views: + + 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 want to inspect a database connection that is not your application's default connection, use the `connection` method: + + $columns = Schema::connection('sqlite')->getColumns('users'); + Using the `db:show` and `db:table` Artisan commands, you can get valuable insight into your database and its associated tables. To see an overview of your database, including its size, type, number of open connections, and a summary of its tables, you may use the `db:show` command: ```shell diff --git a/migrations.md b/migrations.md index 9df9c3a4200..0dadf46168e 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 "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 ba8d2c2ed92..6069d16bfe6 100644 --- a/releases.md +++ b/releases.md @@ -351,3 +351,22 @@ 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 Database and Improved Schema Operations + +_Inspecting Database and Improved Schema Operations was contributed by [Hafez Divandari](https://github.com/hafezdivandari)_ + +Laravel 11 enhances schema operations including native modifying, renaming and dropping columns, advanced spatial types, non-default schema names and new native schema methods to inspect database and its associated tables, views, columns, indexes and foreign keys: + + use Illuminate\Support\Facades\Schema; + + $tables = Schema::getTables(); + + $views = Schema::getViews(); + + $columns = Schema::getColumns('users'); + + $indexes = Schema::getIndexes('users'); + + $foreignKeys = Schema::getForeignKeys('users'); + From b6962f7b63c90b7c41f26c931a4811d582c49978 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 12 Mar 2024 13:53:07 -0500 Subject: [PATCH 2/2] formatting --- database.md | 32 ++++++++++++++------------------ migrations.md | 2 +- releases.md | 10 +++------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/database.md b/database.md index 48c2c46e2b7..c86488477e5 100644 --- a/database.md +++ b/database.md @@ -394,24 +394,6 @@ php artisan db mysql ## Inspecting Your Databases -You may use following schema methods to inspect your database and its associated tables and views: - - 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 want to inspect a database connection that is not your application's default connection, use the `connection` method: - - $columns = Schema::connection('sqlite')->getColumns('users'); - Using the `db:show` and `db:table` Artisan commands, you can get valuable insight into your database and its associated tables. To see an overview of your database, including its size, type, number of open connections, and a summary of its tables, you may use the `db:show` command: ```shell @@ -430,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 0dadf46168e..6df04a5d488 100644 --- a/migrations.md +++ b/migrations.md @@ -278,7 +278,7 @@ You may determine the existence of a table, column, or index using the `hasTable } if (Schema::hasIndex('users', ['email'], 'unique')) { - // The "users" table exists and has a unique index on "email" column... + // The "users" table exists and has a unique index on the "email" column... } diff --git a/releases.md b/releases.md index 6069d16bfe6..ec46860f3b8 100644 --- a/releases.md +++ b/releases.md @@ -352,21 +352,17 @@ 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 Database and Improved Schema Operations +### Inspecting Databases and Improved Schema Operations -_Inspecting Database and Improved Schema Operations was contributed by [Hafez Divandari](https://github.com/hafezdivandari)_ +_Improved schema operations and database inspection was contributed by [Hafez Divandari](https://github.com/hafezdivandari)_ -Laravel 11 enhances schema operations including native modifying, renaming and dropping columns, advanced spatial types, non-default schema names and new native schema methods to inspect database and its associated tables, views, columns, indexes and foreign keys: +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');