Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Inspecting Database #9477

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions database.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');

<a name="table-overview"></a>
#### Table Overview

Expand Down
18 changes: 11 additions & 7 deletions migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ When creating the table, you may use any of the schema builder's [column methods
<a name="determining-table-column-existence"></a>
#### 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...
Expand All @@ -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...
}

<a name="database-connection-table-options"></a>
#### Database Connection and Table Options

Expand All @@ -289,16 +293,16 @@ 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');

// ...
});

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');

// ...
});
Expand Down Expand Up @@ -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).
Expand All @@ -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).

Expand Down Expand Up @@ -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).

Expand Down
15 changes: 15 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

<a name="inspecting-database"></a>
### 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');