Skip to content

Commit 2fd3f07

Browse files
[11.x] Inspecting Database (#9477)
* inspecting database * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4c33dcd commit 2fd3f07

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

database.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ To see how read / write connections should be configured, let's look at this exa
102102
'username' => env('DB_USERNAME', 'root'),
103103
'password' => env('DB_PASSWORD', ''),
104104
'unix_socket' => env('DB_SOCKET', ''),
105-
'charset' => 'utf8mb4',
106-
'collation' => 'utf8mb4_0900_ai_ci',
105+
'charset' => env('DB_CHARSET', 'utf8mb4'),
106+
'collation' => env('DB_COLLATION', 'utf8mb4_0900_ai_ci'),
107107
'prefix' => '',
108108
'prefix_indexes' => true,
109109
'strict' => true,
@@ -412,6 +412,20 @@ If you would like to include table row counts and database view details within t
412412
php artisan db:show --counts --views
413413
```
414414

415+
In addition, you may use the following `Schema` methods to inspect your database:
416+
417+
use Illuminate\Support\Facades\Schema;
418+
419+
$tables = Schema::getTables();
420+
$views = Schema::getViews();
421+
$columns = Schema::getColumns('users');
422+
$indexes = Schema::getIndexes('users');
423+
$foreignKeys = Schema::getForeignKeys('users');
424+
425+
If you would like to inspect a database connection that is not your application's default connection, you may use the `connection` method:
426+
427+
$columns = Schema::connection('sqlite')->getColumns('users');
428+
415429
<a name="table-overview"></a>
416430
#### Table Overview
417431

migrations.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ When creating the table, you may use any of the schema builder's [column methods
267267
<a name="determining-table-column-existence"></a>
268268
#### Determining Table / Column Existence
269269

270-
You may determine the existence of a table or column using the `hasTable` and `hasColumn` methods:
270+
You may determine the existence of a table, column, or index using the `hasTable`, `hasColumn`, and `hasIndex` methods:
271271

272272
if (Schema::hasTable('users')) {
273273
// The "users" table exists...
@@ -277,6 +277,10 @@ You may determine the existence of a table or column using the `hasTable` and `h
277277
// The "users" table exists and has an "email" column...
278278
}
279279

280+
if (Schema::hasIndex('users', ['email'], 'unique')) {
281+
// The "users" table exists and has a unique index on the "email" column...
282+
}
283+
280284
<a name="database-connection-table-options"></a>
281285
#### Database Connection and Table Options
282286

@@ -289,16 +293,16 @@ If you want to perform a schema operation on a database connection that is not y
289293
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:
290294

291295
Schema::create('users', function (Blueprint $table) {
292-
$table->engine = 'InnoDB';
296+
$table->engine('InnoDB');
293297

294298
// ...
295299
});
296300

297301
The `charset` and `collation` properties may be used to specify the character set and collation for the created table when using MySQL:
298302

299303
Schema::create('users', function (Blueprint $table) {
300-
$table->charset = 'utf8mb4';
301-
$table->collation = 'utf8mb4_unicode_ci';
304+
$table->charset('utf8mb4');
305+
$table->collation('utf8mb4_unicode_ci');
302306

303307
// ...
304308
});
@@ -941,7 +945,7 @@ Modifier | Description
941945
`->autoIncrement()` | Set INTEGER columns as auto-incrementing (primary key).
942946
`->charset('utf8mb4')` | Specify a character set for the column (MySQL).
943947
`->collation('utf8mb4_unicode_ci')` | Specify a collation for the column.
944-
`->comment('my comment')` | Add a comment to a column (MySQL/PostgreSQL).
948+
`->comment('my comment')` | Add a comment to a column (MySQL / PostgreSQL).
945949
`->default($value)` | Specify a "default" value for the column.
946950
`->first()` | Place the column "first" in the table (MySQL).
947951
`->from($integer)` | Set the starting value of an auto-incrementing field (MySQL / PostgreSQL).
@@ -951,7 +955,7 @@ Modifier | Description
951955
`->unsigned()` | Set INTEGER columns as UNSIGNED (MySQL).
952956
`->useCurrent()` | Set TIMESTAMP columns to use CURRENT_TIMESTAMP as default value.
953957
`->useCurrentOnUpdate()` | Set TIMESTAMP columns to use CURRENT_TIMESTAMP when a record is updated (MySQL).
954-
`->virtualAs($expression)` | Create a virtual generated column (MySQL / PostgreSQL / SQLite).
958+
`->virtualAs($expression)` | Create a virtual generated column (MySQL / SQLite).
955959
`->generatedAs($expression)` | Create an identity column with specified sequence options (PostgreSQL).
956960
`->always()` | Defines the precedence of sequence values over input for an identity column (PostgreSQL).
957961

@@ -1097,7 +1101,7 @@ Command | Description
10971101
`$table->primary(['id', 'parent_id']);` | Adds composite keys.
10981102
`$table->unique('email');` | Adds a unique index.
10991103
`$table->index('state');` | Adds an index.
1100-
`$table->fullText('body');` | Adds a full text index (MySQL/PostgreSQL).
1104+
`$table->fullText('body');` | Adds a full text index (MySQL / PostgreSQL).
11011105
`$table->fullText('body')->language('english');` | Adds a full text index of the specified language (PostgreSQL).
11021106
`$table->spatialIndex('location');` | Adds a spatial index (except SQLite).
11031107

releases.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,18 @@ Laravel 11 includes improved support for MariaDB. In previous Laravel releases,
351351

352352
For more information on Laravel's database drivers, check out the [database documentation](/docs/{{version}}/database).
353353

354+
<a name="inspecting-database"></a>
355+
### Inspecting Databases and Improved Schema Operations
356+
357+
_Improved schema operations and database inspection was contributed by [Hafez Divandari](https://github.com/hafezdivandari)_
358+
359+
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:
360+
361+
use Illuminate\Support\Facades\Schema;
362+
363+
$tables = Schema::getTables();
364+
$views = Schema::getViews();
365+
$columns = Schema::getColumns('users');
366+
$indexes = Schema::getIndexes('users');
367+
$foreignKeys = Schema::getForeignKeys('users');
368+

0 commit comments

Comments
 (0)