contains() ใน Laravel Collections วิธีเช็คข้อมูลที่มีอยู่ใน Collection
บทนำ
เวลาเราทำงานกับข้อมูลที่อยู่ใน Collection สิ่งที่ต้องทำบ่อยๆ คือ “เช็คว่าข้อมูลนี้มีอยู่ใน Collection หรือเปล่า?” — เช่น เช็คว่า email นี้เคยลงทะเบียนแล้วหรือยัง, เช็คว่าสินค้านี้อยู่ในตะกร้าหรือเปล่า หรือเช็คว่าผู้ใช้มีสิทธิ์นี้หรือเปล่า
Laravel Collections มี method contains() และ doesntContain() ที่ช่วยให้การเช็คทำได้ง่ายและสะอาดมาก
🔧 วิธีใช้งาน contains()
ใช้เช็คค่าเดียว
<?php
$collection = collect([1, 2, 3, 4, 5]);
$collection->contains(3);
// true
$collection->contains(6);
// false
ใช้เช็คหลายค่า
<?php
$collection = collect(['cat', 'dog', 'bird', 'fish']);
$collection->contains('dog');
// true
$collection->contains('rabbit');
// false
ใช้เช็คกับ key-value
<?php
$products = collect([
['name' => 'iPhone', 'price' => 29900],
['name' => 'Samsung', 'price' => 24900],
['name' => 'OPPO', 'price' => 12900],
]);
// เช็คว่ามี product ที่มี name = 'Samsung' อยู่ไหม
$products->contains('name', 'Samsung');
// true
// เช็คว่ามี product ที่มี price = 29900 ไหม
$products->contains('price', 29900);
// true
// เช็คว่ามี product ที่มี price = 19900 ไหม
$products->contains('price', 19900);
// false
ใช้กับ Closure (เงื่อนไขซับซ้อน)
<?php
$users = collect([
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 30],
['name' => 'Bob', 'age' => 35],
]);
// เช็คว่ามี user อายุเกิน 40 ไหม
$users->contains(function ($value, $key) {
return $value['age'] > 40;
});
// false
// เช็คว่ามี user อายุมากกว่า 30 ไหม
$users->contains(function ($value, $key) {
return $value['age'] > 30;
});
// true
🔧 วิธีใช้งาน doesntContain()
doesntContain() คือตรงข้ามกับ contains() — จะ return true ถ้าไม่มีค่าที่ระบุใน Collection
<?php
$collection = collect(['apple', 'banana', 'mango']);
$collection->doesntContain('grape');
// true
$collection->doesntContain('banana');
// false
📌 ตัวอย่างใช้งานจริง
ตัวอย่างที่ 1: เช็คสิทธิ์การเข้าถึง (Authorization)
<?php
$userRoles = collect(['admin', 'editor', 'viewer']);
$user = auth()->user();
if ($userRoles->contains($user->role)) {
return 'มีสิทธิ์เข้าถึง';
}
return 'ไม่มีสิทธิ์เข้าถึง';
ตัวอย่างที่ 2: เช็คสินค้าในตะกร้า
<?php
$cartItems = collect([
['product_id' => 1, 'name' => 'เสื้อยืด', 'qty' => 2],
['product_id' => 5, 'name' => 'กางเกงยีนส์', 'qty' => 1],
]);
// เช็คว่ามีสินค้า product_id = 5 ในตะกร้าหรือเปล่า
if ($cartItems->contains('product_id', 5)) {
echo 'สินค้านี้อยู่ในตะกร้าแล้ว';
}
ตัวอย่างที่ 3: เช็ค email ซ้ำก่อนบันทึก
<?php
$existingEmails = collect(['[email protected]', '[email protected]', '[email protected]']);
$newEmail = '[email protected]';
if ($existingEmails->contains($newEmail)) {
return redirect()->back()->with('error', 'Email นี้ลงทะเบียนแล้ว');
}
return redirect()->back()->with('success', 'ลงทะเบียนสำเร็จ');
ตัวอย่างที่ 4: เช็คค่าว่าง vs ไม่ว่าง
<?php
// ถ้า Collection ว่าง → contains() จะ return false เสมอ
$emptyCollection = collect([]);
$emptyCollection->contains('anything');
// false
// ตรวจสอบ Collection ว่างก่อน
if ($collection->isNotEmpty() && $collection->contains($value)) {
// มีค่านี้อยู่จริง
}
📌 สรุป
contains() และ doesntContain() เป็น method ที่ใช้บ่อยมากในการทำงานกับ Collection โดยเช็คว่าค่าที่ต้องการมีอยู่ใน Collection หรือไม่
สิ่งที่ควรจำ:
– contains($value) — เช็คค่าเดียวใน Collection
– contains($key, $value) — เช็ค key และ value ใน Collection ของ arrays
– contains(Closure) — เช็คด้วยเงื่อนไขที่กำหนดเอง
– doesntContain($value) — ตรงข้ามกับ contains() คือ return true ถ้าไม่มี
ลองนำไปใช้กับงานของคุณได้เลยครับ!