queryBuilder

๋ผ๋ผ๋ฒจ์˜ DB ์ฟผ๋ฆฌ ๋นŒ๋”๋Š” DB์ฟผ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ์šด์˜ํ•˜๋Š”๋ฐ ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•˜๋ฉฐ, PDO ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ SQL ์ธ์ ์…˜์„ ๋ฐฉ์ง€ํ•œ๋‹ค.

์ฟผ๋ฆฌ ์กฐํšŒ

get()

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

get() ๋ฉ”์†Œ๋“œ๋Š” ์•ž์— ์ฒด์ด๋‹ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์˜ ๊ฒฐ๊ณผ๋ฌผ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์†Œ๋“œ๋กœ Collection์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Collection์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ถ€์— items[]๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด empty()๋กœ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ์ด ์•ˆ๋œ๋‹ค. ์ด๋Š” ๋ฐ˜ํ™˜๋œ ์ปฌ๋ ‰์…˜์˜ all()์„ ํ†ตํ•ด ๋‚ด๋ถ€ items๋ฐฐ์—ด์„ ์ ‘๊ทผํ•˜์—ฌ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

foreach ($users as $user) {
    echo $user->name;
}

foreach๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ ์ปฌ๋Ÿผ์— ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

first() / value() / find()

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

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

first()๋ฉ”์„œ๋“œ๋Š” ์•ž์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ•˜๋‚˜์˜ ํ–‰์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  value()๋Š” ์•ž์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์ค‘ value์˜ ์ปฌ๋Ÿผ๋งŒ ์กฐํšŒํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

first()๋Š” ํ•˜๋‚˜์˜ ๋ชจ๋ธ(๊ฐ์ฒด)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  value()๋Š” ๋‹จ์ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

find()

$user = DB::table('users')->find(3);

id๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒ€์ƒ‰ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด find()๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. find()๋„ ๋ชจ๋ธ(๊ฐ์ฒด)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

pluck()

$titles = DB::table('roles')->pluck('title');

ํ…Œ์ด๋ธ”์—์„œ ํ•œ๊ฐœ์˜ ์ปฌ๋Ÿผ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ชจ๋“  ํ…Œ์ด๋ธ”์˜ ์ปฌ๋ ‰์…˜์„ ์กฐํšŒํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

๊ฒฐ๊ณผ ๋ถ„ํ• 

DB::table('users')->where('active', false)
    ->chunkById(100, function ($users) {
        foreach ($users as $user) {
            DB::table('users')
                ->where('id', $user->id)
                ->update(['active' => true]);
        }
    });

๋งŽ์€ ํ–‰์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด chunk()๋ฅผ ์ด์šฉํ•ด ํด๋กœ์ €๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ clousre์˜ ๋ฐ˜ํ™˜๊ฐ’์„ false๋ฅผ ์ฃผ๋ฉด ์ค‘๊ฐ„์— ์ค‘๋‹จ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฒฐ๊ณผ๋ฅผ ์ฒญํ‚น(๋ฒŒํฌ)ํ•˜๋Š”๋ฐ ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ์œ„ํ•ด update์‹œ์—๋Š” chunkById()๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

ํ•จ์ˆ˜

count / max / avg / min / sum

$users = DB::table('users')->count();

$price = DB::table('orders')->max('price');

$price = DB::table('orders')
                ->where('finalized', 1)
                ->avg('price');

exists / doesntExist

return DB::table('orders')->where('finalized', 1)->exists();

return DB::table('orders')->where('finalized', 1)->doesntExist();

๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค.

select

$users = DB::table('users')->select('name', 'email as user_email')->get();

//distinct
$users = DB::table('users')->distinct()->get();

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

๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„๋•Œ๋Š” select๋กœ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

raw query

DB::raw(select * from users)->get();

//selectRaw
$orders = DB::table('orders')
                ->selectRaw('price * ? as price_with_tax', [1.0825])
                ->get();

//whereRoaw
$orders = DB::table('orders')
                ->whereRaw('price > IF(state = "TX", ?, 100)', [200])
                ->get();
//havnigRaw
$orders = DB::table('orders')
                ->select('department', DB::raw('SUM(price) as total_sales'))
                ->groupBy('department')
                ->havingRaw('SUM(price) > ?', [2500])
                ->get();

์ „์ฒด๋‚˜ ์ผ๋ถ€๋ถ„์„ ์ง์ ‘ SQL๋ฌธ์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Join

inner join

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

join()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ inner join์ด ์ˆ˜ํ–‰๋˜๊ณ  ์ด๋•Œ ๋ฉ”์„œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” join์„ ์ˆ˜ํ–‰ํ•  ํ…Œ์ด๋ธ” ์ด๋ฆ„์ด๊ณ  ๊ทธ ์ดํ›„๋Š” join ์ œ์•ฝ์กฐ๊ฑด ์ด๋‹ค.

left / outer join

$users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

$users = DB::table('users')
            ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ” join

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

join์˜ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ํด๋กœ์ €๋ฅผ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌํ…Œ์ด๋ธ”์„ joinํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

์„œ๋ธŒ ์ฟผ๋ฆฌ ์กฐ์ธ

$latestPosts = DB::table('posts')
                   ->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
                   ->where('is_published', true)
                   ->groupBy('user_id');

$users = DB::table('users')
        ->joinSub($latestPosts, 'latest_posts', function ($join) {
            $join->on('users.id', '=', 'latest_posts.user_id');
        })->get();

joinSub, leftJoinSub, rightJoinSub๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์„œ๋ธŒ์ฟผ๋ฆฌ์— ์กฐ์ธ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Union

$first = DB::table('users')
            ->whereNull('first_name');

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

๋ฏธ๋ฆฌ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด๋‘์—ˆ๋‹ค๋ฉด ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฅผ union()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค.

Where

$users = DB::table('users')->where('votes', 100)->get();
$users = DB::table('users')->where('votes', '=', 100)->get();
$users = DB::table('users')->where([
    ['status', '=', '1'],
    ['subscribed', '<>', '1'],
])->get();

where์˜ ๋ฉ”์„œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ธ๊ฐœ๋ฅผ ๊ฐ€์ง€์ง€๋งŒ =์ธ ๊ฒฝ์šฐ์—๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๋ฐฐ์—ด์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ  ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

whereBetween() | whereNotBetween()

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

whereIn()

$users = DB::table('users')
                    ->whereIn('id', [1, 2, 3])
                    ->get();

whereNull()

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

์‚ฝ์ž…

DB::table('users')->insert([
    ['email' => 'taylor@example.com', 'votes' => 0],
    ['email' => 'dayle@example.com', 'votes' => 0],
]);

auto incremet ID ๊ฐ’ ์–ป๊ธฐ

$id = DB::table('users')->insertGetId(
    ['email' => 'john@example.com', 'votes' => 0]
);

์ปฌ๋Ÿผ ๊ฐ’ ์ฆ๊ฐ€

DB::table('users')->increment('votes');

DB::table('users')->increment('votes', 5);

DB::table('users')->decrement('votes');

DB::table('users')->decrement('votes', 5);

์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ชจ๋ธ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

Last updated