![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Bài giảng Phát triển phần mềm nguồn mở: Bài 10 - Nguyễn Hữu Thể
Số trang: 51
Loại file: pdf
Dung lượng: 1.06 MB
Lượt xem: 25
Lượt tải: 0
Xem trước 6 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài giảng này giới thiệu một số tính năng khác của lavarel là database, migrations & seeding, trong đó có một tính năng rất quan trọng đó là migration. Với migration chúng ta có thể tương tác với cấu trúc của database một cách dễ dàng nhưng tạo bảng, xoá bảng, thêm cột, xoá cột, sửa tên cột, thay đổi kiểu dữ liệu…bằng các định nghĩa các file migration bằng code rồi sau đó thực thi thì hệ thống sẽ tự động tạo ra các cấu trúc CSDL cho các bạn. Mời các bạn cùng tham khảo.
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển phần mềm nguồn mở: Bài 10 - Nguyễn Hữu Thể PHÁT TRIỂN PHẦN MỀM NGUỒN MỞ DATABASE, MIGRATIONS & SEEDING Nguyễn Hữu Thể Database ❖ Introduction ❖ Configuration ❖ Read & Write Connections 2 Giới thiệu − Laravel kết nối tới các database và thực thi các query với nhiều database back-ends thông qua sử dụng • raw SQL, • fluent query builder, • Eloquent ORM. − Hiện tại, Laravel hỗ trợ sẵn 4 database: • MySQL • Postgres • SQLite • SQL Server 3 Cấu hình − Thư mục config/database.php. • Trong file này: có thể định nghĩa tất cả các kết nối cơ sở dữ liệu, cũng như chỉ định connection nào là mặc định. ❖ Cấu hình SQL Server 'sqlsrv' => [ 'driver' => 'sqlsrv', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', ], 4 Đọc & ghi các kết nối 'mysql' => [ 'read' => [ 'host' => '192.168.1.1', ], 'write' => [ 'host' => '196.168.1.2' ], 'driver' => 'mysql', 'database' => 'database', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ], 5 Thiết lập database trong file cấu hình chung .env (Tên_Project/.env) APP_ENV=local REDIS_HOST=127.0.0.1 APP_KEY=base64:SPqqJfE1ADzonR REDIS_PASSWORD=null ot2o5g9J8Ix3iRVHsFOclr0KC1KHI= REDIS_PORT=6379 APP_DEBUG=true APP_LOG_LEVEL=debug MAIL_DRIVER=smtp APP_URL=http://localhost MAIL_HOST=mailtrap.io MAIL_PORT=2525 DB_CONNECTION=mysql MAIL_USERNAME=null DB_HOST=127.0.0.1 MAIL_PASSWORD=null DB_PORT=3306 MAIL_ENCRYPTION=null DB_DATABASE=ten_database DB_USERNAME=root PUSHER_APP_ID= DB_PASSWORD= PUSHER_KEY= PUSHER_SECRET= BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync 6 Thực thi lệnh select namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; class UserController extends Controller { public function index() { $users = DB::select('select * from users where active = ?', [1]); return view('user.index', ['users' => $users]); } } Có thể thực thi câu query sử dụng liên kết đặt tên: $results = DB::select('select * from users where id = :id', ['id' => 1]); 7 Thực thi lệnh select Syntax array select(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns array Description Run a select statement against the database. 8 Thực thi câu lệnh insert − Hàm insert nhận câu raw SQL query ở tham số đầu tiên, và bindings ở tham số thứ hai DB::insert('insert into users (id, name) values (?, ?)', [1, ‘Tom']); Syntax bool insert(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns bool Description Run an insert statement against the database. 9 Thực thi câu lệnh update − Hàm update: update các records đang có trong cơ sở dữ liệu. Số lượng row ảnh hưởng bởi câu lệnh sẽ được trả về qua hàm này $affected = DB::update('update users set votes = 100 where name = ?', ['John']); Syntax int update(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns int Description Run an update statement against the database. 10 Thực thi câu lệnh delete − Hàm delete: xoá các records khỏi cơ sở dữ liệu. Giống như update, số lượng dòng bị xoá sẽ được trả về $deleted = DB::delete('delete from users'); Syntax int delete(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns int Description Run a delete statement against the database. 11 Thực thi một câu lệnh chung − Một vài câu lệnh cơ sở dữ liệu không trả về giá trị gì cả. Với những thao tác kiểu này, có thể sử dụng hàm statement trong DB facade DB::statement('drop table users'); 12 Database Example − Table student Column Name Column Datatype Extra Id int(11) Primary key | Auto increment Name varchar(25) − We will see how to add, delet ...
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển phần mềm nguồn mở: Bài 10 - Nguyễn Hữu Thể PHÁT TRIỂN PHẦN MỀM NGUỒN MỞ DATABASE, MIGRATIONS & SEEDING Nguyễn Hữu Thể Database ❖ Introduction ❖ Configuration ❖ Read & Write Connections 2 Giới thiệu − Laravel kết nối tới các database và thực thi các query với nhiều database back-ends thông qua sử dụng • raw SQL, • fluent query builder, • Eloquent ORM. − Hiện tại, Laravel hỗ trợ sẵn 4 database: • MySQL • Postgres • SQLite • SQL Server 3 Cấu hình − Thư mục config/database.php. • Trong file này: có thể định nghĩa tất cả các kết nối cơ sở dữ liệu, cũng như chỉ định connection nào là mặc định. ❖ Cấu hình SQL Server 'sqlsrv' => [ 'driver' => 'sqlsrv', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', ], 4 Đọc & ghi các kết nối 'mysql' => [ 'read' => [ 'host' => '192.168.1.1', ], 'write' => [ 'host' => '196.168.1.2' ], 'driver' => 'mysql', 'database' => 'database', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ], 5 Thiết lập database trong file cấu hình chung .env (Tên_Project/.env) APP_ENV=local REDIS_HOST=127.0.0.1 APP_KEY=base64:SPqqJfE1ADzonR REDIS_PASSWORD=null ot2o5g9J8Ix3iRVHsFOclr0KC1KHI= REDIS_PORT=6379 APP_DEBUG=true APP_LOG_LEVEL=debug MAIL_DRIVER=smtp APP_URL=http://localhost MAIL_HOST=mailtrap.io MAIL_PORT=2525 DB_CONNECTION=mysql MAIL_USERNAME=null DB_HOST=127.0.0.1 MAIL_PASSWORD=null DB_PORT=3306 MAIL_ENCRYPTION=null DB_DATABASE=ten_database DB_USERNAME=root PUSHER_APP_ID= DB_PASSWORD= PUSHER_KEY= PUSHER_SECRET= BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync 6 Thực thi lệnh select namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; class UserController extends Controller { public function index() { $users = DB::select('select * from users where active = ?', [1]); return view('user.index', ['users' => $users]); } } Có thể thực thi câu query sử dụng liên kết đặt tên: $results = DB::select('select * from users where id = :id', ['id' => 1]); 7 Thực thi lệnh select Syntax array select(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns array Description Run a select statement against the database. 8 Thực thi câu lệnh insert − Hàm insert nhận câu raw SQL query ở tham số đầu tiên, và bindings ở tham số thứ hai DB::insert('insert into users (id, name) values (?, ?)', [1, ‘Tom']); Syntax bool insert(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns bool Description Run an insert statement against the database. 9 Thực thi câu lệnh update − Hàm update: update các records đang có trong cơ sở dữ liệu. Số lượng row ảnh hưởng bởi câu lệnh sẽ được trả về qua hàm này $affected = DB::update('update users set votes = 100 where name = ?', ['John']); Syntax int update(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns int Description Run an update statement against the database. 10 Thực thi câu lệnh delete − Hàm delete: xoá các records khỏi cơ sở dữ liệu. Giống như update, số lượng dòng bị xoá sẽ được trả về $deleted = DB::delete('delete from users'); Syntax int delete(string $query, array $bindings = array()) •$query(string) – query to execute in database Parameters •$bindings(array) – values to bind with queries Returns int Description Run a delete statement against the database. 11 Thực thi một câu lệnh chung − Một vài câu lệnh cơ sở dữ liệu không trả về giá trị gì cả. Với những thao tác kiểu này, có thể sử dụng hàm statement trong DB facade DB::statement('drop table users'); 12 Database Example − Table student Column Name Column Datatype Extra Id int(11) Primary key | Auto increment Name varchar(25) − We will see how to add, delet ...
Tìm kiếm theo từ khóa liên quan:
Phần mềm nguồn mở Phát triển phần mềm nguồn mở Thiết lập database Thực thi lệnh select Migration structure Rolling back migrationsTài liệu liên quan:
-
183 trang 322 0 0
-
'Phần mềm tự do và phần mềm nguồn mở' Free and Open Source Software – Asia-Pacific Consultation
5 trang 135 0 0 -
Xây dựng hệ thống tích hợp liên tục nội bộ sử dụng công cụ nguồn mở Jenkins và Gitlab
11 trang 93 0 0 -
Giáo trình Nhập môn quản trị hệ thống Linux
145 trang 48 0 0 -
25 trang 47 0 0
-
Bài giảng Phần mềm nguồn mở: Bài 1 - Đoàn Thiện Ngân
29 trang 45 0 0 -
Bài giảng Phần mềm nguồn mở: Bài 3 - Đoàn Thiện Ngân
12 trang 45 0 0 -
Bài giảng Nhập môn công nghệ thông tin và truyền thông: Bài 7 - GV. Lê Thanh Hương
29 trang 41 0 0 -
Bài giảng Phát triển phần mềm nguồn mở: Bài 8 - Nguyễn Hữu Thể
56 trang 33 0 0 -
Đề cương ôn tập học kì 1 môn Tin học lớp 11 năm 2023-2024 - Trường THPT Hoàng Văn Thụ, Hà Nội
7 trang 33 0 0