Giải pháp chuyển đổi số trọn gói cho doanh nghiệp Việt
Ở 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.
-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:
Illuminate\Support\Facades\Schema
Cú pháp:
Schema::table('tablename', function ($table) {
//code
});
Vd: Tạo bảng users.
Schema::table('users', function ($table) {
//code
});
Cú Pháp:
Schema::rename($from, $to);
VD: Đổi bảng users thành admin.
Schema::rename('users','admin');
Cú Pháp:
Schema::drop('tableName');
//hoac
Schema::dropIfExists('tableName');
VD: Xóa bảng users.
Schema::drop('users');
//hoặc
Schema::dropIfExists('users');
Cú Pháp:
Schema::table('tableName', function ($table) {
$table->typeData('columnName');
});
VD: Thêm cột email với kiểu dữ liệu vachar.
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.
Schema: table('tableName', function ($table) {
$table->renameColumn('from', 'to');
});
VD: Đổi cột email trong bảng users thành usermail.
Schema::table('users', function ($table) {
$table->renameColumn('email', 'usermail');
});
Cú Pháp:
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.
Schema::table('users', function ($table) {
$table->dropColumn('email');
});
Cú Pháp:
$table->index('coumnName');
Các kiểu Index.
| Cú pháp | Mô 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.
$table->unique('email');
Cú Pháp:
| Cú Pháp | Mô 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. |
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.
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ì .
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
Cú Pháp
$table->dropForeign('columnName');
VD: Mình xóa khóa ngoại ở ví dụ trên đi.
$table->dropForeign('user_id');
-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?
Chưa có câu hỏi nào cho bài này.