SlideShare a Scribd company logo
CHUYÊN ĐỀ ỨNG DỤNG
LẬP TRÌNH WEB 1
(229154)
Quey Builder
trong Laravel
https://laravel.com/
2
Nội dung
❑ Tìm hiểu về Quey Builder
❑ Cấu hình kết nối database
❑ Một số phương thức dung trong query builder
❑ Truy xuất dữ liệu
3
Kết nối cơ sở dữ liệu với PHP:
o MySQLi (procedural)
o MySQLi (object-oriented)
o PDO (PHP Data Objects)
▪ Laravel kết nối đến nhiều loại database qua PDO (Mysql, SQL
server, sqllite,….)
https://laravel.com/
4
▪ Laravel tương tác với cơ sở dữ liệu, có 2 kiểu truy vấn phổ biến:
Tìm hiểu về Quey Builder
Query Builder Eloquent ORM
Object Relational Mapping
https://laravel.com/
5
▪ Query Builder cung cấp interface (Class – method) tương tác với cơ
sở dữ liệu
▪ Để sử dụng query builder thông qua class DB
▪ Sử dụng lệnh bên dưới trước khi dùng DB:
Tìm hiểu về Quey Builder
use IlluminateSupportFacadesDB;
https://laravel.com/
6
Cấu hình kết nối database
▪ Trong File .env
▪ DB_HOST : địa chỉ server chứa mysql
▪ DB_PORT: port của database server Mysql (default 3306)
▪ DB_DATABASE: têndatabase
▪ DB_USERNAME: username kết nối database
▪ DB_PASSWORD: mật khẩu để kết nối database
Query Builder
https://laravel.com/
7
Query lấy dữ liệu trong table
❑ Lấy tất cả dữ liệu trong 1 table
▪ DB::table('tableName')
=> Tạo query : Select * from tableName
Query Builder
=> Dùng phương thức get để lấy ra dữ liệu của câu truy vấn
▪ DB::table('tableName')->get()
https://laravel.com/
8
Query lấy dữ liệu trong table
❑ Lấy tất cả dữ liệu trong 1 table
Query Builder
https://laravel.com/
9
Query lấy dữ liệu trong table
❑ Lấy tất cả dữ liệu trong 1 table
Query Builder
https://laravel.com/
10
Query lấy dữ liệu trong table
❑ Xem câu lệnh SQL sử dụng toSql()
Query Builder
https://laravel.com/
11
Query lấy dữ liệu trong table
❑ Xác định các column sẽ lấy trong table
DB::table('tableName')
->select('colName1', 'colName2', 'colNameN')
->get()
Query Builder
https://laravel.com/
12
Query lấy dữ liệu trong table
❑ Xác định các column sẽ lấy trong table
Query Builder
https://laravel.com/
13
Query lấy dữ liệu trong table
❑ Xác định các column sẽ lấy trong table và đặt
bí danh cho column
Query Builder
DB::table('tableName')
->select('colName1 as newName', 'colName2', 'colNameN')
->get()
https://laravel.com/
14
Query lấy dữ liệu trong table
▪ Xác định các column sẽ lấy trong table và đặt bí danh
cho column
Query Builder
https://laravel.com/
15
Query lấy dữ liệu trong table
❑ Câu lệnh với where
DB::table('tableName')
->where ('colName', 'operator', value )
Query Builder
▪ operator : =, >=, <=, <>, like
https://laravel.com/
16
Query lấy dữ liệu trong table
❑ Câu lệnh với where ( =)
Query Builder
https://laravel.com/
17
Query lấy dữ liệu trong table
❑ Câu lệnh với where (<>)
Query Builder
https://laravel.com/
18
Query lấy dữ liệu trong table
❑ Câu lệnh với where - >=, <=
Query Builder
https://laravel.com/
19
Query lấy dữ liệu trong table
❑ Câu lệnh với where - like
Query Builder
https://laravel.com/
20
Query lấy dữ liệu trong table
❑ Câu lệnh với where – nhiều điều kiện - and
Query Builder
https://laravel.com/
21
Query lấy dữ liệu trong table
❑ Câu lệnh với where – nhiều điều kiện - and
Query Builder
Truyền một mảng điều kiện
https://laravel.com/
22
Query lấy dữ liệu trong table
❑ Câu lệnh với where – kết hợp or
Query Builder
https://laravel.com/
23
Query lấy dữ liệu trong table
❑ Câu lệnh với where – kết hợp or
Query Builder
https://laravel.com/
24
Query lấy dữ liệu trong table
❑ Câu lệnh với where – In
Query Builder
https://laravel.com/
25
Query lấy dữ liệu trong table
❑ Câu lệnh với where – Not In
Query Builder
https://laravel.com/
26
Query lấy dữ liệu trong table
▪ whereNull('colName')
▪ whereNotNull('colName’)
▪ whereDate
▪ whereMonth
▪ whereDay
▪ …………..
Query Builder
Xem thêm tại
https://laravel.com/docs/10.x/queries
https://laravel.com/docs/<version mới nhất>/queries
https://laravel.com/
27
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có order by
https://laravel.com/
28
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có order by
https://laravel.com/
29
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có order by, limit
https://laravel.com/
30
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có order by, offset, limit
https://laravel.com/
31
Query lấy dữ liệu trong table
Query Builder
=> Dùng phương thức get để lấy danh sách các row từ thực thi câu truy vấn
DB::table('tableName’)
->….
->get()
DB::table('tableName’)
->….
->first()
=> Dùng phương thức first để lấy row đầu tiên từ thực thi câu truy vấn
https://laravel.com/
32
Query lấy dữ liệu trong table
Query Builder
DB::table('tableName’)
->….
->first()
=> Dùng phương thức first để lấy row đầu tiên từ thực thi câu truy vấn
https://laravel.com/
33
Query lấy dữ liệu trong table
Query Builder
https://laravel.com/
34
Query lấy dữ liệu trong table
Query Builder
❑ Kiểm tra sự tồn tại của dữ liệu
▪ exists ()
=> trả về true nếu có dữ liệu thỏa câu truy vấn
▪ doesntExist() : ngược lại
https://laravel.com/
35
Query lấy dữ liệu trong table
Query Builder
❑ Kiểm tra sự tồn tại của dữ liệu
▪ isEmpty()
=> trả về true nếu không có bản ghi nào trong Collection
=> nếu có thì trả false
▪ isNotEmpty() : ngược lại
https://laravel.com/
36
Query lấy dữ liệu trong table
Query Builder
❑ Một số phương thức khác
Xem thêm tại
https://laravel.com/docs/10.x/queries
https://laravel.com/docs/<version mới nhất>/queries
▪ max
▪ min
▪ avg
▪ sum
▪ …………..
▪ distinct
▪ groupBy
▪ having
https://laravel.com/
37
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có join
▪ inner join
▪ left join
▪ right join
▪ cross join
https://laravel.com/
38
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có join
▪ inner join
https://laravel.com/
39
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có join
▪ inner join
https://laravel.com/
40
Query lấy dữ liệu trong table
Query Builder
❑ Câu lệnh có join
▪ left join
https://laravel.com/
41
Insert, Update, Delete dữ liệu trong table
▪ Thêm (insert) 1 record
Query Builder
▪ Thêm (insert) nhiều record
https://laravel.com/
42
Insert, Update, Delete dữ liệu trong table
▪ Đối với bảng có column Id là Auto-Incrementing
▪ Thêm (insert) 1 record và trả về id record vừa thêm
Query Builder
https://laravel.com/
43
Insert, Update, Delete dữ liệu trong table
▪ Cập nhật record (update)
=> trả về số lượng record được update
Query Builder
https://laravel.com/
44
Insert, Update, Delete dữ liệu trong table
▪ Cập nhật record (update)
=> trả về số lượng record được update
Query Builder
https://laravel.com/
45
Insert, Update, Delete dữ liệu trong table
▪ Xóa record (delete)
=> trả về số lượng record được delete
Query Builder
https://laravel.com/
46
Query Builder
Xem thêm tại
https://laravel.com/docs/10.x/queries
https://laravel.com/docs/<version mới nhất>/queries

More Related Content

PPTX
E learning lab - Tim hieu Cake PHP
PPT
04 ado
PDF
Bài 5 : Lập trình thực thi các hoạt động xử lý theo cơ chế ngắt kết nối (Disc...
PDF
Bài 3 : Truy vấn và cập nhật CSDL bằng
PDF
Bai3 basic servlets_956
PDF
Co So du lieu chuong 4 truong Dai Hoc hcm
PDF
PHP.pdf
PDF
BÀI 6: THỰC THI CÁC HOẠT ĐỘNG XỬ LÝ THEO CƠ CHẾ NGẮT KẾT NỐI BẰNG CÁCH SỬ DỤ...
E learning lab - Tim hieu Cake PHP
04 ado
Bài 5 : Lập trình thực thi các hoạt động xử lý theo cơ chế ngắt kết nối (Disc...
Bài 3 : Truy vấn và cập nhật CSDL bằng
Bai3 basic servlets_956
Co So du lieu chuong 4 truong Dai Hoc hcm
PHP.pdf
BÀI 6: THỰC THI CÁC HOẠT ĐỘNG XỬ LÝ THEO CƠ CHẾ NGẮT KẾT NỐI BẰNG CÁCH SỬ DỤ...

Recently uploaded (8)

PDF
bai_giang_plc_va_mang_cong_nghiep_chuong_1_9949.pdf
PPTX
BS Trung-CBL viêm da mạn tính_Y5 _Tại lớp.pptx
PDF
bài giảng báo cáo kế hoạch kinh doanh.pdf
PDF
bai_giang_plc_va_mang_cong_nghiep_chuong_3_0174.pdf
DOCX
LEC 1.docx cơ sở vật lý của sự vận chuyển chất qua màng
PDF
bÃ-i-4.pdf Bnaia sjnshsjiddujeudbdbieneudb
PDF
nguyen tac su dung thuoc tdddddddhoa.pdf
PDF
bai_giang_plc_va_mang_cong_nghiep_chuong_6_0654.pdf
bai_giang_plc_va_mang_cong_nghiep_chuong_1_9949.pdf
BS Trung-CBL viêm da mạn tính_Y5 _Tại lớp.pptx
bài giảng báo cáo kế hoạch kinh doanh.pdf
bai_giang_plc_va_mang_cong_nghiep_chuong_3_0174.pdf
LEC 1.docx cơ sở vật lý của sự vận chuyển chất qua màng
bÃ-i-4.pdf Bnaia sjnshsjiddujeudbdbieneudb
nguyen tac su dung thuoc tdddddddhoa.pdf
bai_giang_plc_va_mang_cong_nghiep_chuong_6_0654.pdf
Ad
Ad

01_CDLTW1_Slide77.pdf1- setup backend-test.pdf1- setup backend-test.pdf