Giải pháp chuyển đổi số trọn gói cho doanh nghiệp Việt

Schema Buider trong Laravel - Tài liệu lập trình laravel

Tài liệu lập trình laravel

← Toàn bộ tài liệu


Bài 15: Schema Buider trong Laravel

Bài 15/28 Xem dạng sách

Ở các bài trước mình đã giới thiệu với mọi người cách truy xuất dữ liệu trong Laravel Với Query Buider và Eloquent ORM rồi. Hôm nay tiếp tục với series Học Laravel của Toidicode.com mình xin được giới thiệu với mọi người một thuật ngữ mới nữa trong Laravel đó là Schema Builder.

 

1, Schema Builder trong Laravel là gì?

 

-Schema Builder Là một class trong bộ Facades của Laravel nó giúp chúng ta làm việc với tất cả các cơ sở dữ liệu mà Laravel hỗ trợ với các hàm được định nghĩa sẵn.

-Thường thì nó sẽ được kết hợp với Migrations để xây dự cấu trúc database.

-Điều kiện để sử dụng Schema Builder:

  • Phải có kết nối với database .
  • Muốn sử dụng được Schema Builder thì phải gọi namespace của nó Illuminate\Support\Facades\Schema

2, Các lệnh thường dùng trong Schema Builder.

 

Tạo bảng.

Cú pháp:

php
copy
Schema::table('tablename', function ($table) {
    //code
});

Vd: Tạo bảng users.

php
copy
Schema::table('users', function ($table) {
   //code
});

Đổi tên bảng.

Cú Pháp:

php
copy
Schema::rename($from, $to);

VD: Đổi bảng users thành admin.

php
copy
Schema::rename('users','admin');

Xóa bảng.

Cú Pháp:

php
copy
Schema::drop('tableName');
//hoac
Schema::dropIfExists('tableName');

VD: Xóa bảng users.

php
copy
Schema::drop('users');
//hoặc
Schema::dropIfExists('users');

Thêm cột.

Cú Pháp:

php
copy
Schema::table('tableName', function ($table) {
    $table->typeData('columnName');
});

VD: Thêm cột email với kiểu dữ liệu vachar.

php
copy
Schema::table('users', function ($table) {
    $table->string('email');
});

->Chú ý: Các kiểu dữ liệu mình sẽ nói rõ hơn ở bài sau.

Đổi tên cột.

php
copy
Schema: table('tableName', function ($table) {
    $table->renameColumn('from', 'to');
});

VD: Đổi cột email trong bảng users thành usermail.

php
copy
Schema::table('users', function ($table) {
    $table->renameColumn('email', 'usermail');
});

Xóa cột.

Cú Pháp:

php
copy
Schema::table('tableName', function ($table) {
    $table->dropColumn('columnName');
    //hoặc xóa nhiều cột cùng lúc.
    $table->dropColumn(['columnName1','columnName2','columnName3']);
});

VD: xóa cột email trong bảng users.

php
copy
Schema::table('users', function ($table) {
    $table->dropColumn('email');
});

Tạo index.

Cú Pháp:

php
copy
$table->index('coumnName');

Các kiểu Index.

Cú phápMô tả
table->primary('id'); Add a primary key.
table->primary(['first', 'last']); Add composite keys.
$table->unique('email'); Add a unique index.
$table->unique('state', 'my_index_name'); Add a custom index name.
$table->unique(['first', 'last']); Add a composite unique index.
$table->index('state'); Add a basic index.

-Mấy cái này dịch sang tiếng việt rất khó biểu diễn nên mình để tiếng anh cho dễ :D.

-VD: mình sẽ đánh unique index cho cột email.

php
copy
$table->unique('email');

Xóa Index.

Cú Pháp:

Cú PhápMô Tả
$table->dropPrimary('users_id_primary'); Drop a primary key from the "users" table.
$table->dropUnique('users_email_unique'); Drop a unique index from the "users" table.
$table->dropIndex('geo_state_index'); Drop a basic index from the "geo" table.

Thêm khóa ngoại.

Cú Pháp:

Schema::table('tableName', function ($table) {
    $table->integer('conmName')->unsigned();

    $table->foreign('Conmumname')->references('columnName')->on('tableName2');
});

Vd: Mình sẽ thêm cột users_id của bảng posts làm khóa ngoại trên bảng users.

php
copy
Schema::table('posts', function ($table) {
    $table->integer('user_id')->unsigned();

    $table->foreign('user_id')->references('id')->on('users');
});

Hoặc mình muốn khi xóa mội users thì đồng thời sẽ xóa hết các bài đăng của users đó đi thì .

php
copy
$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');

Xóa khóa ngoại.

Cú Pháp

php
copy
$table->dropForeign('columnName');

VD: Mình xóa khóa ngoại ở ví dụ trên đi.

php
copy
$table->dropForeign('user_id');

5,Lời kết.

-Sau khi đọc hết phần trên các bạn có thấy Laravel hỗ trợ chúng ta rất mạnh về cấu trúc của database đúng không?

Sơ đồ quan hệ các bảng

Table Name: {{table.name}}
Num Name Type
{{field_index+1}}
{{field.name}} {{field.type}}

Hỏi đáp

Gửi với tên {{comment_full_name}}

Chưa có câu hỏi nào cho bài này.

Zalo 0936.006.058 Hotline 0936.006.058