query builder

find() – จะคล้ายๆกับ first() แต่ค้าหาแค่กับ id ที่เป็น primary key เท่านั้น

$posts = DB::table('posts')
->find(1001);
{#1323 ▼ // app\Http\Controllers\PostController.php:57
  +"id": 1001
  +"user_id": 6
  +"title": "Itaque quis nobis occaecati et amet quia."
  +"slug": "itaque-quis-nobis-occaecati-et-amet-quia"
  +"description": "Id enim qui eligendi nihil."
  +"excerpt": "Tenetur ullam vel alias ratione veniam."
  +"is_published": 1
  +"min_to_read": 7
  +"created_at": "2023-05-12 17:44:52"
  +"updated_at": "2023-05-12 17:44:52"
  +"deleted_at": null
}

pluck()

$posts = DB::table('posts')
->pluck('title');
#items: array:15 [▼
  0 => "Consequatur repellendus consequuntur illum qui."
  1 => "Culpa sed repellat voluptatem libero dolorum."
  2 => "Dolore nostrum molestiae molestiae maxime."
  3 => "Est quae aliquam tempore nostrum voluptates."
  4 => "Et dolorum quam optio iste."
  5 => "Et voluptas accusantium ipsum inventore officiis veritatis ratione."
  6 => "Itaque quis nobis occaecati et amet quia."
  7 => "Maxime aut minima consectetur debitis enim dolores."
  8 => "Modi aliquam voluptate impedit similique dicta cupiditate voluptates."
  9 => "Molestias cum vel ut saepe officia ad."
  10 => "Nisi mollitia doloribus id dolores quas."
  11 => "Omnis illo qui quisquam rerum ullam ut."
  12 => "Reiciendis ut veritatis eos labore accusamus eius nemo."
  13 => "Sed dolores itaque dolor tenetur quis non dignissimos."
  14 => "Vero qui corporis aut ea sit asperiores."
]

value() – จะคล้ายๆกับ first() แต่จะถึงมาแค่ คอลัมเดียวเท่านั้น ต่างจาก first() ที่มาเป็นทั้ง Object เลย

$posts = DB::table('posts')
->where('id',1001)
->value('title');
"Itaque quis nobis occaecati et amet quia."

Insert

แบบ 1 Row คืนค่าเป็น true ถ้าเพิ่มได้ แต่ถ้าติดเงื่อนไข จะ error เลย เช่น field ที่ห้ามซ้ำ ดูใช้ยากนิดนึง ถ้าไม่ได้เขียนดักค่าไว้ก่อน

$posts = DB::table('posts')
    ->insert(
        [
            'user_id' => 1,
            'title' => 'Insert through the DB facade 2',
            'slug' => 'insert-through-the-db-facade-2',
            'excerpt' => 'excerpt',
            'description' => 'description',
            'is_published' => true,
            'min_to_read' => 2,
        ]
    );
$posts = true

แบบ 2 Row จะคล้ายแบบ 1 Row คืนค่าเป็น true เมื่อเพิ่มสำเร็จ และerror เมื่อติดปัญหา
*** ไม่ได้คืนค่ามาเป็น จำนวน Row  ที่เริ่มเข้าไปได้นะครับ

$posts = DB::table('posts')
    ->insert(
        [
            [
                'user_id' => 1,
                'title' => 'Insert through the DB facade 5',
                'slug' => 'insert-through-the-db-facade-5',
                'excerpt' => 'excerpt',
                'description' => 'description',
                'is_published' => true,
                'min_to_read' => 2,
            ],
            [
                'user_id' => 1,
                'title' => 'Insert through the DB facade 6',
                'slug' => 'insert-through-the-db-facade-6',
                'excerpt' => 'excerpt',
                'description' => 'description',
                'is_published' => true,
                'min_to_read' => 2,
            ]
        ]
    );
$posts = true

insertOrIgnore คล้ายๆกับ insert เพิ่มข้อมูล และคืนค่ามาเป็นคำนวน Row ที่เพิ่มเข้าไปได้
ถ้าเพิ่มไม่ได้ก็คืนค่ามาเป็น 0 และข้อมูลที่เพิ่มเข้าไปไม่ได้ เพราะอะไรก็แล้วแต่เช่น  ห้ามมีค่าซ้ำในคอลัมน์ ก็จะไม่มีการแสดงข้อผิดพลาดออกมา
ถือว่าเป็นการเพิ่มข้อมูลที่น่าสนใจมากๆ แต่จะเน้นไปที่การเพิ่มข้อมูลเท่านั้น คือถ้าเพิ่มได้ก็เพิ่ม เพิ่มไม่ได้ผิดเงื่อนไขก็จะข้ามไป
อย่างถ้าเพิ่มเข้ามา 5 record  แต่ มี3 record ที่มีปัญหาคีย์ซ้ำ มันก็จะเพิ่มให้ 2 record ที่ไม่ติดปัญหา และคืนค่ามาเป็น 2

$posts = DB::table('posts')
    ->insertOrIgnore(
        [
            'user_id' => 1,
            'title' => 'Insert through the DB facade 20',
            'slug' => 'insert-through-the-db-facade-20',
            'excerpt' => 'excerpt',
            'description' => 'description',
            'is_published' => true,
            'min_to_read' => 2,
        ]
    );
$posts = 1

อันนี้แบบเพิ่มเข้าไปหลายๆ record พร้อมๆกัน

$posts = DB::table('posts')
            ->insertOrIgnore(
                [
                    [
                        'user_id' => 1,
                        'title' => 'Insert through the DB facade 5',
                        'slug' => 'insert-through-the-db-facade-5',
                        'excerpt' => 'excerpt',
                        'description' => 'description',
                        'is_published' => true,
                        'min_to_read' => 2,
                    ],
                    [
                        'user_id' => 1,
                        'title' => 'Insert through the DB facade 6',
                        'slug' => 'insert-through-the-db-facade-6',
                        'excerpt' => 'excerpt',
                        'description' => 'description',
                        'is_published' => true,
                        'min_to_read' => 2,
                    ]
                ]
            );
posts = 2

upsert
ใช้สำหรับอัพเดทข้อมูลเดิมที่มี หรือถ้าไม่มีข้อมูลนี้อยู่ ก็จะเป็นการเพิ่มเข้าไปใหม่เลย โดยเราต้องกำหนด field ที่จะใช้เช็ค ว่า record นี้มีอยู่หรือไม่
มีความคล้ายกับ firstOrCreate(), updateOrCreate()

แต่การใช้ต้องระวังนิดนึง กรณีที่มีการเช็ค 2 field 2 พร้อมกัน เพราะตอนตรวจสอบจะตรวจสอบทั้ง 2 field พร้อมกัน ถ้า field  ใด field  หนึ่งไม่ตรงเท่ากับว่า ไม่พบข้อมูลเดิม จะเป็นการสั่งให้เพิ่มเป็นข้อมูลใหม่ แต่ข้อมูลใหม่ที่เพิ่มเข้าไปนี้ ถ้ามี field ใด field หนึ่ง ที่กำหนดเป็น unique ไว้ก็พ้น error ออกมาเลย อันนี้ต้องระวัง

หลังจากเช็ค field  unique แล้วถ้าเป็นการอัพเดทข้อมูล ถ้าข้อมูล เหมือนเดิมทุกอย่าง คือ record นี้มีแล้ว และข้อมูลที่ส่งเข้ามาก็เหมือนเดิม อันนี้จะคืนค่าเป็น 0

การคืนค่า
แบบหลาย record
ส่งเข้าไป 4 record เพิ่มใหม่ทั้งหมด – คืนค่าเป็น 4
ส่งเข้าไป 4 record ซ้ำทั้งหมด – คืนค่าเป็น 0
ส่งเข้าไป 4 record เพิ่มใหม่ 2 ซ้ำ 2 – คืนค่าเป็น 2
ส่งเข้าไป 4 record อัพเดท 2 – คืนค่าเป็น 4
ส่งเข้าไป 4 record อัพเดท 4 – คืนค่าเป็น 8
** การอัพเดทเหมือนจะคืนค่ามาเป็น 2 ต่อ 1 record (อัพเดทกี่ field ก็ค่าเท่ากัน)

การเพิ่ม แบบ 1 Record

$posts = DB::table('posts')
    ->upsert(
        [
            'user_id' => 1,
            'title' => 'Post Testing 777',
            'slug' => 'post-testing-777',
            'excerpt' => 'excerpt Post Testing',
            'description' => 'description Post Testing',
            'is_published' => true,
            'min_to_read' => 2,
        ],['title','slug']
    );
$psots = 1

การเพิ่ม มากกว่า 1 Record ส่งค่าเข้าไป แบบ Array

$posts = DB::table('posts')
    ->upsert(
        [
            [
                'user_id' => 1,
                'title' => 'Post Testing 666',
                'slug' => 'post-testing-666',
                'excerpt' => 'excerpt Post Testing',
                'description' => 'description Post Testing',
                'is_published' => true,
                'min_to_read' => 2,
            ],
            [
                'user_id' => 1,
                'title' => 'Post Testing 555',
                'slug' => 'post-testing-555',
                'excerpt' => 'excerpt Post Testing',
                'description' => 'description Post Testing',
                'is_published' => true,
                'min_to_read' => 2,
            ]
        ],['title','slug']
    );
$posts = 2

insertGetId
ถ้าเพิ่มสำเร็จ จะคืนค่ามาเป็น id ของ record ที่เพิ่มเข้า ถ้าผิดเงื่อน db จะพ้น error
เหมาะกับงานที่ใช้เพิ่มข้อมุลเข้ารัวๆ ไม่ได้มีความจำเป็นต้องตรวจสอบอะไรมากมาย

$posts = DB::table('posts')
    ->insertGetId(
        [
            'user_id' => 1,
            'title' => 'Post Testing 444',
            'slug' => 'post-testing-444',
            'excerpt' => 'excerpt Post Testing',
            'description' => 'description Post Testing',
            'is_published' => true,
            'min_to_read' => 2,
        ]
    );
$posts = 1120 (int)

 

update
ใช้อัพเดทข้อมูลเดิม และสามารถใช้ orWhere หรือเงื่อนไขอื่นได้ กรณีที่ต้องการแก้ไขหลายๆ Record

การคืนค่าจะคืนค่ามาเป็น จำนวน Record ที่ได้รับการอัพเดท
ถ้า where มา 5 Record และอัพเดททั้งหมด ก็จะคืนค่า = 5
ถ้า where มา 5 Record และอัพเดทไปแค่ 3 อีก 2 ข้อมูลที่ส่งเข้าไปมันเหมือนเดิมไม่ได้เปลี่ยนแปลงอะไร ก็จะคืนค่า = 2
ถ้า where มา 5 Record และข้อมูลมันเหมือนเดิมเลย ไม่ได้อัพเดทอะไรก็จะคืนค่าเป็น = 0
สรุปคือ คืนค่ามาเฉพาะจำนวน Record ที่ได้รับการอัพเดท เท่านั้น

$posts = DB::table('posts')
    ->where('id', 1120)
    ->orWhere('id', 1118)
    ->orWhere('id', 1117)
    ->update(
        [
            'excerpt' => 'excerpt Post 99',
            'description' => 'description 99',
        ]
    );
$psots = 3

 

increment , decrement
ใช้เพิ่มค่าหรือลดค่า โดยต้องระบุชื่อ field ลงไป โดย default จะเพิ่มค่า แค่ครั้งละ 1  ถ้าต้องการค่าอื่นกำหนดลงไปได้เลย
หลังจากรันแล้ว จะคืนค่ามาเป็นจำนวน Record ที่ได้รับการอัพเดท

  • ทดสอบ  increment ใน Record  ที่ where มาไม่ได้ หรือไม่มี Record ค่าที่ได้กลับมาคือ 0 และไม่พ่น Error
$posts = DB::table('posts')
    ->where('id', 1120)
    ->increment('min_to_read');
$psots = 1
value min_to_read = min_to_read + 1

ตัวอย่างการกำหนดค่าแบบเฉพาะ

$posts = DB::table('posts')
    ->where('id', 1120)
    ->increment('min_to_read',20);
$posts = 1
value min_to_read = min_to_read + 20

ตัวอย่างการเพิ่มหลาย หลาย Record พร้อมกัน

$posts = DB::table('posts')
    ->where('id', 1120)
    ->orWhere('id', 1118)
    ->orWhere('id', 1117)
    ->increment('min_to_read');
$posts = 3
value min_to_read = min_to_read + 1

ทดลองใส่แบบซ้ำ ค่าที่ได้คือ ทำการอัพเดทแค่ 1 ครั้งเท่านั้น

$posts = DB::table('posts')
    ->where('id', 1120)
    ->where('id', 1120)
    ->increment('min_to_read');
$posts = DB::table('posts')
    ->where('id', 1120)
    ->orWhere('id', 1120)
    ->increment('min_to_read');
$posts = 1
value min_to_read = min_to_read + 1

ทดสอบกับ field ที่ไม่ int จะพ่น error เลย

$posts = DB::table('posts')
    ->where('id', 1120)
    ->increment('title');

 

incrementEach
ใช้กรณีต้องการ ทำพร้อมกัน หลาย field โดนกำหนด field เข้าเป็น array

$posts = DB::table('posts')
    ->where('id', 1120)
    ->incrementEach(['min_to_read','min_to_read']);

updateOrInsert
อันนี้ยังไม่ค่อยเคลียนัก แต่จากข้อจำกัดที่มี สรุปได้ประมาณนี้
รับข้อมูล 2 ตัว แบบ array

ข้อมูลตัวที่ 1 ข้อมุลของ field ที่ต้องการอัพเดท
ข้อมูลตัวที่ 2 ข้อมูลของ field ที่เป็นข้อจำกัดว่าต้องมีเช่นพวก field ที่ห้าม NULL

ด้วยข้อจำกัดของเงื่อนไข เลยทดสอบยังไม่ครบ แต่เข้าใจว่า
เหาะกับงานที่ ไม่ได้กำหนดเงื่อนไขไว้เช่นพวก field นี้ห้ามซ้ำอะไรพวกนี้
งานที่เหมาะคืองานที่ มีกำหนดแค่ id อย่างเดียวที่จำเป็น ค่านอกนั้น จึงใช้ตรวจสอบ

ทดสอบ ด้วยการกำหนด field ที่เป็นข้อมูลใหม่
และกำหนดข้อมูลตัวที่ 2 เป็นข้อมูลที่จำเป็นต้องมี แต่ไม่ซ้ำกับข้อมูลเดิม

$posts = DB::table('posts')
    ->updateOrInsert(
        [
            'excerpt' => 'excerpt Post 100',
            'description' => 'description 100',
        ],
        [
            'user_id' => 1,
            'title' => 'Insert through the DB facade 100',
            'slug' => 'insert-through-the-db-facade-100',
        ]
    );
true
และได้ Record ใหม่

ทดสอบต่อจากข้างบน โดยแก้ไขข้อมูลส่วนที่ 1 เท่านั้น
สิ่งที่ได้ คือ พ่น Error เพราะติดข้อจำกัดของ feild ของส่วนที่ 2 ที่ห้ามซ้ำมันเลย เพิ่มใหม่ไม่ได้

$posts = DB::table('posts')
    ->updateOrInsert(
        [
            'excerpt' => 'excerpt Post 1500',
            'description' => 'description 1500',
        ],
        [
            'user_id' => 1,
            'title' => 'Insert through the DB facade 100',
            'slug' => 'insert-through-the-db-facade-100',
        ]
    );

delete ใช้คู่กับ where คือค่าเป็น int จำนวน Record ที่ลบออกไปได้

$posts = DB::table('posts')
    ->where('id',1123)
    ->delete();
$posts = 1

ทดสอบลบแบบหลายๆ Record

$posts = DB::table('posts')
    ->where('id','>',1012)
    ->delete();
$posts = 35
จำนวน Record ที่ลบไป

ทดสอบทด Record ที่ไม่มีอยู่ ได้ค่าเป็น 0 ไม่มีพ่น Error

$posts = DB::table('posts')
    ->where('id',1012)
    ->delete();
$posts = 0

 

truncate คำสั่งอันตราย ใช้ลบข้อมูลทั้งหมดใน ตาราง

$posts = DB::table('posts')
    ->truncate();
$posts = null

 

count ใช้นับจำนวน Record โดยสามารถใช้กับพวก where ได้
คืนว่ามาเป้นตัวเลข และถ้านับไม่ได้ก็คืนมาเป็น 0

$posts = DB::table('posts')
    ->count();
$posts = 35
ตามจำนวน Record

สามารถใช้คู่กับ where ได้ เพื่อเลือกข้อมูล ก่อนทำการ นับ

$posts = DB::table('posts')
    ->where('is_published', 1)
    ->count();
$posts = 5
Record ที่มี  is_published = 1

sum ใช้หาผลรวม คืนค่าเป็นตัวเลข และใช้รวมกับ where ได้
และต้องเลือก field ที่จะ sum ด้วย
มีทดลองใช้ sum กับ field เป็น string จะคืนค่ามาเป็น 0

$posts = DB::table('posts')
    ->sum('min_to_read');

$posts = DB::table('posts')
    ->where('is_published', 1)
    ->sum('min_to_read');

 

avg ใช้หาค่าเฉพาะ โดยต้องกำหนด field ด้วย
มีทดลองใช้ กับ field เป็น string จะคืนค่ามาเป็น 0.0

$posts = DB::table('posts')
    ->avg('min_to_read');
$posts = 5.1429

 

min และ max ใช้หาค่าที่มากที่สุด โดยกำหนด field ที่จะใช้หาค่า

$posts = DB::table('posts')
    ->max('min_to_read');

$posts = DB::table('posts')
    ->min('min_to_read');

ทดสอบหาค่ากับ field ที่เป็น string ได้ด้วย
แต่ค่าที่ได้ ไม่ใช่การนับตัวอักษรนะ เป็นเป็นแค่ลำกับ A-Z ตามการเรียลลำดับของ string เท่านั้น

$posts = DB::table('posts')
    ->min('title');

$posts = DB::table('posts')
    ->max('title');
min = Ab vel molestias totam nihil autem commodi est.
max = Voluptatem voluptas aliquam ea omnis nostrum.

ข้อมูลตัวอย่างที่ใช้ทดสอบ

id        name                display     weight
1         vivo V27            AMOLED      180
2         Redmi 12Pro         AMOLED      187
3         Xiaomi 13Pro        AMOLED      229
4         Huawei P60          OLED        206
5         Apple iPhone14      AMOLED      204
6         ASUS ROG Phone6     AMOLED      239

exists : ใช้ตรวจสอบการมีอยู่ของข้อมูล โดยตรงใช้คู่กับ where
doesntExist : เหมือนกันแค่ตรงกันข้าม
ทดสอบแล้วว่าใช้กับ find ไม่ได้

ทดสอบถึงข้อมูลที่มีอยู่

$result = DB::table('phones')
    ->where('id', 5)
    ->exists();

$result = DB::table('phones')
    ->where('name', 'Huawei P60')
    ->orWhere('name', 'ASUS')
    ->exists();
$result = true

ทดสอบ ถึงข้อมูลที่ไม่มีอยู่

$result = DB::table('phones')
    ->where('id', 30)
    ->exists();

$result = DB::table('phones')
    ->where('name', 'oppo')
    ->orWhere('name', 'samsung')
    ->exists();
$result = false

 

 

 

 

dump : คำสั่งในการ Debugging น่าสนใจ

$result = DB::table('phones')
    ->where('name', 'vivo')
    ->dump();
"select * from `phones` where `name` = ?"

array:1 [▼
  0 => "vivo"
]

 

chunk : คำสั่งสำหรับการทำงานกับข้อมูล จำนวนมากๆ โดยจะทำการดึงข้อมุลมาเป็นส่วนๆ และเอาข้อมูลส่วนั้นไปจัดการก่อน แล้วค่อยดึงมาเพิ่มใหม่ เพื่อวนทำอีกครั้ง

DB::table('posts')
    ->orderBy('id')
    ->chunk(150, function ($posts) {
        foreach ($posts as $post) {
            echo $post->title;
        }
    });

 

raw : ใช้เขีนคำสั่ง ที่ซัพซอน โดยส่งเป็นคำสั่ง sql เข้าไป

// selectRaw()
DB::table('posts')
    ->selectRaw('count(*) as post_count')
    ->first();

// whereRaw()
DB::table('posts')
    ->whereRaw('created_at > NOW() - INTERVAL 1 DAY')
    ->get();

// havingRaw()
DB::table('posts')
    ->select('user_id', DB::raw('SUM(min_to_read) as total_time'))
    ->groupBy('user_id')
    ->havingRaw('SUM(min_to_read) > 10')
    ->get();

// orderByRaw()
DB::table('posts')
    ->orderByRaw('created_at DESC')
    ->get();

// groupByRaw()
DB::table('posts')
    ->select('user_id', DB::raw('AVG(rating) as avg_rating'))
    ->groupByRaw('user_id')
    ->get();

 

Exit mobile version