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