Query Builder ( ฟังก์ชันที่ใช้จัดการฐานข้อมูล )

คือการจัดการๆคิวรี่ของ laravel ช่วยอำนวยความสะดวกให้เรา ไม่ต้องเขียนคิวรี่ยาวๆ ด้วยตัวเองครับ <br /><br /> <a name="selects"></a> ## การเลือกข้อมูล

ดึงค่าทั้งหมดจากตาราง users

1 $users = DB::table('users')->get();
2 
3 foreach ($users as $user)
4 {
5 	var_dump($user->name);
6 }

<br /><br /> ดึงค่าแถวแรกจากตาราง users โดย name เท่ากับ john

1 $user = DB::table('users')->where('name', 'John')->first();
2 
3 var_dump($user->name);

<br /><br /> ดึงค่าจากตาราง users โดย name เท่ากับ john แล้วก็เอาแค่คอลัมน์ที่ชื่อว่า name

1 $name = DB::table('users')->where('name', 'John')->pluck('name');

<br /><br /> ดึงค่าทั้งหมดจากตาราง roles โดยเอาแค่คอลัมน์ title

1 $roles = DB::table('roles')->lists('title');

ค่าที่คืนมาจะเป็นอาเรย์นะครับ ถ้าเราอยากใส่คีย์ให้แต่ละแถวเราใส่พารามิเตอร์ตัวที่สองเข้าไป name จะไปเป็นคีย์ให้กับ title

1 $roles = DB::table('roles')->lists('title', 'name');

เมทอด select ใช้กำหนดคำสั่งในการเลือกเอง

1 $users = DB::table('users')->select('name', 'email')->get();
2 
3 $users = DB::table('users')->distinct()->get();
4 
5 $users = DB::table('users')->select('name as user_name')->get();

เลือกข้อมูลจากผลการคิวรี่อีกที

1 $query = DB::table('users')->select('name');
2 
3 $users = $query->addSelect('age')->get();

การใช้ where

1 $users = DB::table('users')->where('votes', '>', 100)->get();

การใช้หลายๆเงื่อนไขโดยวิธีเชนเมทอด

1 $users = DB::table('users')
2                     ->where('votes', '>', 100)
3                     ->orWhere('name', 'John')
4                     ->get();

ใช้ between

1 $users = DB::table('users')
2                     ->whereBetween('votes', array(1, 100))->get();

<br /><br /> ตัวอย่างการใช้ where กับ In ร่วมกัน

1 $users = DB::table('users')
2                     ->whereIn('id', array(1, 2, 3))->get();
3 
4 $users = DB::table('users')
5                     ->whereNotIn('id', array(1, 2, 3))->get();

ใช้หาแถวที่เป็นค่าวางตามคอลัมน์

1 $users = DB::table('users')
2                     ->whereNull('updated_at')->get();

ตัวอย่างการเรียงลำดับข้อมูล

1 $users = DB::table('users')
2                     ->orderBy('name', 'desc')
3                     ->groupBy('count')
4                     ->having('count', '>', 100)
5                     ->get();

การจำกัดข้อมูล

1 $users = DB::table('users')->skip(10)->take(5)->get();

<br /><br /><br /><br /> <a name="joins"></a> ## Joins

ตัวอย่างการ join ครับ

1 DB::table('users')
2             ->join('contacts', 'users.id', '=', 'contacts.user_id')
3             ->join('orders', 'users.id', '=', 'orders.user_id')
4             ->select('users.id', 'contacts.phone', 'orders.price');

การจอยแบบเติมคิวรี่ลงไปช่วยประหยัดเวลา เวลาคิดไม่ออกว่าจะใช้ฟังก์ไหนดี แทรกคิวรี่ลงไปตรงๆ เลย:

1 DB::table('users')
2         ->join('contacts', function($join)
3         {
4         	$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
5         })
6         ->get();

<a name="advanced-wheres"></a> ## Advanced Wheres

การ where แบบ หลายเงื่อนไข

1 DB::table('users')
2             ->where('name', '=', 'John')
3             ->orWhere(function($query)
4             {
5             	$query->where('votes', '>', 100)
6             	      ->where('title', '<>', 'Admin');
7             })
8             ->get();

หน้าตาของคิวรี่จะออกมาเป็นแบบนี้

1 select * from users where name = 'John' or (votes > 100 and title <> 'Admin')

หาว่ามีค่านี้อยู่ไหม

1 DB::table('users')
2             ->whereExists(function($query)
3             {
4             	$query->select(DB::raw(1))
5             	      ->from('orders')
6             	      ->whereRaw('orders.user_id = users.id');
7             })
8             ->get();

<br /><br /><br /><br /> หน้าตาของคิวรี่จะออกมาเป็นแบบนี้

1 select * from users
2 where exists (
3 	select 1 from orders where orders.user_id = users.id
4 )

<a name="aggregates"></a> ## Aggregates การหาผลรวมชนิดต่างๆ

ตัวอย่างการคำนวนค่า

1 $users = DB::table('users')->count();
2 
3 $price = DB::table('orders')->max('price');
4 
5 $price = DB::table('orders')->min('price');
6 
7 $price = DB::table('orders')->avg('price');
8 
9 $total = DB::table('users')->sum('votes');

<a name="raw-expressions"></a> ## Raw Expressions

Raw คือการใส่คิวรี่แบบสดๆ เข้าไปเลยไม่ต้องไปให้ฟังก์ชันสร้างให้ ประหยัดเวลามากขึ้นเมื่อเราต้องการค้นหาแบบซับซ้อน

ตัวอย่างการใช้คำสั่งคิวรี่

1 $users = DB::table('users')
2                      ->select(DB::raw('count(*) as user_count, status'))
3                      ->where('status', '<>', 1)
4                      ->groupBy('status')
5                      ->get();

<br /><br /><br /><br /> การเพิ่มและลดค่าให้คอลัมน์

1 DB::table('users')->increment('votes');
2 
3 DB::table('users')->increment('votes', 5);
4 
5 DB::table('users')->decrement('votes');
6 
7 DB::table('users')->decrement('votes', 5);

การกำหนดค่าคอลัมน์ที่จะเพิ่มค่าให้

1 DB::table('users')->increment('votes', 1, array('name' => 'John'));

<a name="inserts"></a> ## การเพิ่มข้อมูล

ตัวอย่าง

1 DB::table('users')->insert(
2 	array('email' => 'john@example.com', 'votes' => 0)
3 );

การเพิ่มค่าพร้อมกับเพิ่มค่า id ด้วย

1 $id = DB::table('users')->insertGetId(
2 	array('email' => 'john@example.com', 'votes' => 0)
3 );

หมายเหตุ: ถ้าใช้ PostgreSQL เมทอด insertGetId คาดหวังว่าจะใช้คอลัมน์ “id” เป็นตัวที่มันจะเพิ่มให้

การเพิ่มหลายๆข้อมูล

1 DB::table('users')->insert(array(
2 	array('email' => 'taylor@example.com', 'votes' => 0),
3 	array('email' => 'dayle@example.com', 'votes' => 0),
4 ));

<br /><br /><br /><br /> <a name="updates"></a> ## การแก้ไข

ตัวอย่าง

1 DB::table('users')
2             ->where('id', 1)
3             ->update(array('votes' => 1));

<a name="deletes"></a> ## การลบ

ตัวอย่าง

1 DB::table('users')->where('votes', '<', 100)->delete();

การลบข้อมูลทั้งหมดเป็นการลบแบบแถวต่อแถว

1 DB::table('users')->delete();

การลบแบบลบทั้งตารางด้วยแล้วสร้างขึ้นใหม่

1 DB::table('users')->truncate();

<a name="unions"></a> ## การทำ Unions

union คือการจับผลลัพท์ของการ selecet 2 ครั้ง มารวมกันเป็นหนึ่งผลลัพท์ การทำ union ใน querie

1 $first = DB::table('users')->whereNull('first_name');
2 
3 $users = DB::table('users')->whereNull('last_name')->union($first)->get();

มีเมทอด unionAll ใช้งานเหมือนกับ union เลยครับ <br /><br /><br /><br /> <a name="pessimistic-locking"></a> ## การยึดทรัพยากรเพื่อใช้งาน

ใน 4.1 นี้ได้เพิ่ม เมทอด sharedLock เพื่อช่วยในการทำ transaction

1 DB::table('users')->where('votes', '>', 100)->sharedLock()->get();

เมทอด lockForUpdate ใช้ในการยึดข้อมูลที่ จะทำการแแก้ไขจนกว่าจะเสร็จครับ

1 DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();

<a name="caching-queries"></a> ## Caching Queries

เราสามารถทำการแคชหรือบันทึกผลการคิวรี่ไว้บน session ก่อนด้วยเมทอด remember

ตัวอย่าง

1 $users = DB::table('users')->remember(10)->get();

ในตัวอย่างเราจะแคชผลการค้นหานี้เป็นเวลา 10 นาที ระหว่างนี้การคิวรี่จากตัวอย่างจะไม่ไปดึงข้อมูลจากฐานข้อมูล แต่จะดึงจากแคชจนกว่าจะหมดเวลาครับ