Laravel มี Query Builder ที่ช่วยให้เขียน SQL ได้ง่ายขึ้นโดยไม่ต้องใช้คำสั่ง SQL ตรงๆ
ก่อนใช้งานต้อง import
use Illuminate\Support\Facades\DB;
SELECT – ดึงข้อมูล
ดึงข้อมูลทั้งหมด (SELECT * FROM users
)
$users = DB::table('users')->get();
foreach ($users as $user) {
echo $user->name . "<br>";
}
ได้ผลลัพธ์เป็น
Collection
ของออบเจ็กต์
ดึงข้อมูลแค่บางคอลัมน์ (SELECT name, email FROM users
)
$users = DB::table('users')->select('name', 'email')->get();
ดึงข้อมูลแบบมีเงื่อนไข (WHERE id = 1
)
$user = DB::table('users')->where('id', 1)->first();
echo $user->name;
first()
ดึงแค่ 1 แถวแรก
ดึงข้อมูลหลายเงื่อนไข (WHERE name = 'John' AND email = 'john@example.com'
)
$user = DB::table('users')
->where('name', 'John')
->where('email', 'john@example.com')
->first();
ใช้ orWhere
(WHERE name = 'John' OR email = 'john@example.com'
)
$users = DB::table('users')
->where('name', 'John')
->orWhere('email', 'john@example.com')
->get();
ใช้ whereBetween
(WHERE age BETWEEN 18 AND 25
)
$users = DB::table('users')
->whereBetween('age', [18, 25])
->get();
INSERT – เพิ่มข้อมูล
เพิ่มข้อมูลใหม่ (INSERT INTO users (name, email) VALUES (?, ?);
)
DB::table('users')->insert([
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => bcrypt('password'),
]);
เพิ่มหลายรายการพร้อมกัน (INSERT INTO users (...) VALUES (...), (...);
)
DB::table('users')->insert([
['name' => 'Alice', 'email' => 'alice@example.com', 'password' => bcrypt('123456')],
['name' => 'Bob', 'email' => 'bob@example.com', 'password' => bcrypt('123456')],
]);
UPDATE – อัปเดตข้อมูล
อัปเดตข้อมูล (UPDATE users SET name = 'John Updated' WHERE id = 1;
)
DB::table('users')
->where('id', 1)
->update(['name' => 'John Updated']);
เพิ่มค่าให้คอลัมน์ (UPDATE users SET age = age + 1;
)
DB::table('users')->increment('age');
ใช้
increment()
และ decrement()
แทน SET column = column + 1
DELETE – ลบข้อมูล
ลบข้อมูล (DELETE FROM users WHERE id = 1;
)
DB::table('users')->where('id', 1)->delete();
ลบข้อมูลทั้งหมด (TRUNCATE TABLE users;
)
DB::table('users')->truncate();
คำสั่งนี้จะลบข้อมูลทั้งหมดโดยไม่สามารถกู้คืนได้!
ORDER BY และ LIMIT
เรียงลำดับ (ORDER BY created_at DESC
)
$users = DB::table('users')
->orderBy('created_at', 'desc')
->get();
จำกัดจำนวนแถว (LIMIT 5
)
$users = DB::table('users')
->limit(5)
->get();
ข้ามแถวแรกและดึงแค่ 10 แถว (OFFSET 10 LIMIT 10
)
$users = DB::table('users')
->offset(10)
->limit(10)
->get();
JOIN – รวมตาราง
INNER JOIN (JOIN profiles ON users.id = profiles.user_id
)
$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->select('users.*', 'profiles.bio')
->get();
LEFT JOIN (LEFT JOIN profiles ON users.id = profiles.user_id
)
$users = DB::table('users')
->leftJoin('profiles', 'users.id', '=', 'profiles.user_id')
->get();
GROUP BY และ HAVING
นับจำนวนผู้ใช้แต่ละสถานะ (GROUP BY status
)
$users = DB::table('users')
->select('status', DB::raw('COUNT(*) as total'))
->groupBy('status')
->get();
กรองกลุ่มข้อมูล (HAVING COUNT(*) > 10
)
$users = DB::table('users')
->select('status', DB::raw('COUNT(*) as total'))
->groupBy('status')
->having('total', '>', 10)
->get();
ใช้ Transactions
DB::transaction(function () {
DB::table('users')->where('id', 1)->update(['balance' => DB::raw('balance - 100')]);
DB::table('users')->where('id', 2)->update(['balance' => DB::raw('balance + 100')]);
});
ถ้าคำสั่งใดล้มเหลว ทั้งหมดจะถูก rollback
สรุป
คำสั่ง SQL | Query Builder |
---|---|
SELECT * FROM users; | DB::table('users')->get(); |
SELECT name FROM users WHERE id = 1; | DB::table('users')->where('id', 1)->first(); |
INSERT INTO users (name, email) VALUES ('John', 'john@example.com'); | DB::table('users')->insert([...]); |
UPDATE users SET name = 'John Updated' WHERE id = 1; | DB::table('users')->where('id', 1)->update([...]); |
DELETE FROM users WHERE id = 1; | DB::table('users')->where('id', 1)->delete(); |
ORDER BY created_at DESC; | DB::table('users')->orderBy('created_at', 'desc')->get(); |
JOIN profiles ON users.id = profiles.user_id; | DB::table('users')->join('profiles', 'users.id', '=', 'profiles.user_id')->get(); |
Query Builder ใช้งานง่ายและปลอดภัยจาก SQL Injection
รองรับการใช้งานกับหลายฐานข้อมูล
ใช้ร่วมกับ Eloquent ได้
เหมาะสำหรับงานที่ต้องการความยืดหยุ่นสูง!
Leave a Reply