Danh mục

Bài giảng Phát triển phần mềm nguồn mở: Bài 11 - Nguyễn Hữu Thể

Số trang: 57      Loại file: pdf      Dung lượng: 1.12 MB      Lượt xem: 22      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 trang bị cho người học kiến thức về database, migrations & seeding. Những nội dung chính được trình bày trong chương này gồm có: Database, generating migrations, migration structure, running migrations, rolling back migrations, writing seeders, running seeders. 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 11 - 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, dele ...

Tài liệu được xem nhiều: