├── .gitattributes └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | *.sql linguist-detectable=true 2 | *.sql linguist-language=sql 3 | *.sql text 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SQL queries generated by Eloquent ORM reference 2 | 3 | [Get](#get) 4 | 5 | [Where](#where) 6 | 7 | [Create](#create) 8 | 9 | [Update](#update) 10 | 11 | [Delete](#delete) 12 | 13 | [One to One](#one-to-one) 14 | 15 | [One to Many](#one-to-many) 16 | 17 | [Many to Many](#many-to-many) 18 | 19 | [Aggregate functions](#aggregate-functions) 20 | 21 | [Miscellaneous](#miscellaneous) 22 | 23 | 24 | 25 | ## Get 26 | 27 | `get` (`all`) 28 | 29 |
User::get()
30 | SELECT * FROM users
31 |
32 |
33 | `get` (`all`) with soft deletes on
34 | User::get()SELECT * FROM users WHERE users.deleted_at IS NULL
35 |
36 |
37 | `get` with soft deleted rows
38 | User::withTrashed()->get()SELECT * FROM users
39 |
40 |
41 | `first`
42 | User::first()SELECT * FROM users LIMIT 1
43 |
44 |
45 | `find`
46 | User::find(2)SELECT * FROM users WHERE users.id = 2 LIMIT 1
47 |
48 |
49 | `findMany`
50 | User::findMany([1, 2, 3])SELECT * FROM users WHERE users.id IN (1, 2, 3)
51 |
52 |
53 | `value`
54 | User::where('name', 'John Smith')->value('email')SELECT email FROM users WHERE name = 'John Smith' LIMIT 1
55 |
56 |
57 | `paginate`
58 | User::paginate()
59 | ```sql
60 | SELECT COUNT(*) AS aggregate FROM users
61 | SELECT * FROM users LIMIT 15 OFFSET 0
62 | ```
63 |
64 |
65 |
66 |
67 | ## Where
68 |
69 | `where`
70 | User::where('age', '>', 32)->get()SELECT * FROM users WHERE `age` > 32
71 |
72 |
73 | `orWhere`
74 | User::where('age', '>', 40)->orWhere('age', '<', 20)->get()SELECT * FROM users WHERE age > 40 OR age < 20
75 |
76 |
77 | `whereIn`
78 | User::whereIn('age', [20, 30, 40])->get()SELECT * FROM users WHERE age IN (20, 30, 40)
79 |
80 |
81 | `whereBetween`
82 | User::whereBetween('age', [20,40])->get()SELECT * FROM users WHERE age BETWEEN 20 AND 40
83 |
84 |
85 | `whereDate`
86 | User::whereDate('created_at', now())->get()SELECT * FROM `users` WHERE DATE(created_at) = '2023-10-04'
87 |
88 |
89 |
90 |
91 | ## Create
92 |
93 | `create`
94 | User::create(['name' => 'John', 'email' => 'john@smith.com'])
95 | ```sql
96 | INSERT INTO users (name, email, updated_at, created_at)
97 | VALUES ('John', 'john@smith.com', '2023-10-04 15:30:34', '2023-10-04 15:30:34')
98 | ```
99 |
100 |
101 | `firstOrCreate`
102 | User::firstOrCreate(['email' => 'john@smith.com'], ['name' => 'John'])
103 | ```sql
104 | SELECT * FROM users WHERE (email = 'john@smith.com') LIMIT 1
105 | INSERT INTO users (email, name, updated_at, created_at)
106 | VALUES ('john@smith.com', 'John', '2023-10-04 16:17:59', '2023-10-04 16:17:59')
107 | ```
108 |
109 |
110 |
111 |
112 | ## Update
113 |
114 | `UPDATE`
115 | User::where('status', 'some_status')->update(['type' => 'some_type'])
116 | ```sql
117 | UPDATE users SET type = 'some_type', users.updated_at = '2023-10-04 15:36:44'
118 | WHERE status = 'some_status'
119 | ```
120 |
121 |
122 | Update single row
123 | $user->update(['name' => 'John', 'email' => 'john@smith.com'])
124 | ```sql
125 | UPDATE users SET name = 'John', email = 'john@smith.com', users.updated_at = '2023-10-04 16:12:30'
126 | WHERE id = 1
127 | ```
128 |
129 |
130 | `updateOrCreate`
131 | User::updateOrCreate(['email' => 'john@smith.com'], ['name' => 'James']);
132 | ```sql
133 | SELECT * FROM users WHERE (email = 'john@smith.com') LIMIT 1
134 | UPDATE users SET name = 'James', users.updated_at = '2023-10-04 16:14:18'
135 | WHERE id = 1
136 | ```
137 |
138 |
139 |
140 |
141 | ## Delete
142 |
143 | `delete`
144 | User::where('status', 'some_status')->delete()DELETE FROM users WHERE status = 'some_status'
145 |
146 |
147 | Delete with soft deletes on
148 | User::where('status', 'some_status')->delete()
149 | ```sql
150 | UPDATE users SET deleted_at = '2023-10-25 12:58:26', users.updated_at = '2023-10-25 12:58:26'
151 | WHERE status = 'some_status' AND users.deleted_at IS NULL
152 | ```
153 |
154 |
155 | Delete single row
156 | $user->delete()DELETE FROM users WHERE id = 1
157 |
158 |
159 | Delete single row with soft deletes on
160 | $user->delete()
161 | ```sql
162 | UPDATE users SET deleted_at = '2023-10-25 12:59:52', users.updated_at = '2023-10-25 12:59:52'
163 | WHERE id = 1
164 | ```
165 |
166 |
167 | `destroy`
168 | User::destroy(1, 2, 3)
169 | ```sql
170 | SELECT * FROM users WHERE id IN (1, 2, 3)
171 | DELETE FROM users WHERE id = 1
172 | DELETE FROM users WHERE id = 2
173 | DELETE FROM users WHERE id = 3
174 | ```
175 |
176 |
177 | Destroy with soft deletes on
178 | User::destroy(1, 2, 3)
179 | ```sql
180 | SELECT * FROM users WHERE id IN (1, 2, 3) AND users.deleted_at IS NULL
181 | UPDATE users SET deleted_at = '2023-10-25 12:54:53', users.updated_at = '2023-10-25 12:54:53' WHERE id = 1
182 | UPDATE users SET deleted_at = '2023-10-25 12:54:53', users.updated_at = '2023-10-25 12:54:53' WHERE id = 2
183 | UPDATE users SET deleted_at = '2023-10-25 12:54:53', users.updated_at = '2023-10-25 12:54:53' WHERE id = 3
184 | ```
185 |
186 |
187 | Restore soft deleted row
188 | $user->restore()UPDATE users SET deleted_at = '', users.updated_at = '2023-10-25 13:07:24' WHERE id = 1
189 |
190 |
191 |
192 |
193 | ## One to One
194 |
195 | `profiles` table has `id`, `user_id`, `city`
196 |
197 | User `hasOne` profile, profile `belongsTo` user
198 |
199 | For nested relationships examples another one to one relationship is used. Profile `hasOne` passport, passport `belongsTo` profile
200 |
201 | Get user's profile
202 | $user->profile or $user->profile()->first()SELECT * FROM profiles WHERE profiles.user_id = 2 AND profiles.user_id IS NOT NULL LIMIT 1
203 |
204 |
205 | Get user with profile
206 | User::with('profile')->find(2)
207 | ```sql
208 | SELECT * FROM users WHERE users.id = 2 LIMIT 1
209 | SELECT * FROM profiles WHERE profiles.user_id IN (2)
210 | ```
211 |
212 |
213 | Get user's passport (nested one to one relationships)
214 | $user->profile->passport
215 | ```sql
216 | SELECT * FROM users WHERE users.id = 2 LIMIT 1
217 | SELECT * FROM profiles WHERE profiles.user_id = 2 AND profiles.user_id IS NOT NULL LIMIT 1
218 | SELECT * FROM passports WHERE passports.profile_id = 4 AND passports.profile_id IS NOT NULL LIMIT 1
219 | ```
220 |
221 |
222 | Get user with profile AND passport (nested one to one relationships)
223 | User::with('profile.passport')->find(2)
224 | ```sql
225 | SELECT * FROM users WHERE users.id = 2 LIMIT 1
226 | SELECT * FROM profiles WHERE profiles.user_id IN (2)
227 | SELECT * FROM passports WHERE passports.profile_id IN (4)
228 | ```
229 |
230 |
231 | Get users who have profiles
232 | User::has('profile')->get()
233 | ```sql
234 | SELECT * FROM users WHERE EXISTS
235 | (SELECT * FROM profiles WHERE users.id = profiles.user_id)
236 | ```
237 |
238 |
239 | Get users who have passport (nested one to one relationships)
240 | User::has('profile.passport')->get()
241 | ```sql
242 | SELECT * FROM users WHERE EXISTS
243 | (SELECT * FROM profiles WHERE users.id = profiles.user_id AND EXISTS
244 | (SELECT * FROM passports WHERE profiles.id = passports.profile_id)
245 | )
246 | ```
247 |
248 |
249 | Get users from Adelaide
250 | ```php
251 | User::whereHas('profile', function ($q) {
252 | $q->where('city', 'Adelaide');
253 | })->get();
254 | ```
255 | ```sql
256 | SELECT * FROM users WHERE EXISTS
257 | (SELECT * FROM profiles WHERE users.id = profiles.user_id AND city = 'Adelaide')
258 | ```
259 |
260 |
261 | Get users who have profiles and load their profiles
262 | User::has('profile')->with('profile')->get()
263 | ```sql
264 | SELECT * FROM users WHERE EXISTS
265 | (SELECT * FROM profiles WHERE users.id = profiles.user_id)
266 | SELECT * FROM profiles WHERE profiles.user_id IN (1, 2, 3)
267 | ```
268 |
269 |
270 |
271 |
272 | ## One to Many
273 |
274 | `orders` table has `id`, `user_id`, `comment`
275 |
276 | User has many orders. Order `belongsTo` user
277 |
278 | For nested relationships examples another one to many relationship is used. Order `hasMany` support tickets, support ticket `belongsTo` order.
279 |
280 | Get user's orders
281 | $user->orders or $user->orders()->get()SELECT * FROM orders WHERE orders.user_id = 1 AND orders.user_id IS NOT NULL
282 |
283 |
284 | Get users with orders
285 | User::with('orders')->get()
286 | ```sql
287 | SELECT * FROM users
288 | SELECT * FROM orders WHERE orders.user_id IN (1, 2, 3)
289 | ```
290 |
291 |
292 | Get users with orders and support tickets (nested one to many relationships)
293 | User::with('orders.supportTickets')->get()
294 | ```sql
295 | SELECT * FROM users
296 | SELECT * FROM orders WHERE orders.user_id IN (1, 2, 3)
297 | SELECT * FROM support_tickets WHERE support_tickets.order_id IN (7, 8, 9)
298 | ```
299 |
300 |
301 | Load companies for user
302 | $user->load('orders')SELECT * FROM orders WHERE orders.user_id IN (1)
303 |
304 |
305 | Get users who have orders
306 | User::has('orders')->get()
307 | ```sql
308 | SELECT * FROM users WHERE EXISTS
309 | (SELECT * FROM orders WHERE users.id = orders.user_id)
310 | ```
311 |
312 |
313 | Get users who have support tickets (nested one to many relationships)
314 | User::has('orders.supportTickets')->get()
315 | ```sql
316 | SELECT * FROM users WHERE EXISTS
317 | (SELECT * FROM orders WHERE users.id = orders.user_id AND EXISTS
318 | (SELECT * FROM support_tickets WHERE orders.id = support_tickets.order_id))
319 | ```
320 |
321 |
322 | Get users who has orders with empty comment
323 | ```php
324 | User::whereHas('orders', function ($q) {
325 | $q->whereNull('comment');
326 | })->get()
327 | ```
328 | ```sql
329 | SELECT * FROM users WHERE EXISTS
330 | (SELECT * FROM orders WHERE users.id = orders.user_id AND comment IS NULL)
331 | ```
332 |
333 |
334 | Get users who have orders and load their orders
335 | User::has('orders')->with('orders')->get()
336 | ```sql
337 | SELECT * FROM users WHERE EXISTS
338 | (SELECT * FROM orders WHERE users.id = orders.user_id)
339 | SELECT * FROM orders WHERE orders.user_id IN (1, 2, 3)
340 | ```
341 |
342 |
343 |
344 |
345 | ## Many to Many
346 |
347 | `companies` table has `id`, `name`
348 |
349 | `company_user` is a pivot table, it has `company_id`, `user_id` columns
350 |
351 | User `belongsToMany` companies, company `belongsToMany` users
352 |
353 | For nested relationships examples another many to many relationship is used. Company `belongsToMany` employees, employee `belongsToMany` companies
354 |
355 | Get user's companies
356 | $user->companies or user->companies()->get()
357 | ```sql
358 | SELECT companies.*, company_user.user_id AS pivot_user_id, company_user.company_id AS pivot_company_id
359 | FROM companies
360 | INNER JOIN company_user ON companies.id = company_user.company_id
361 | WHERE company_user.user_id = 1
362 | ```
363 |
364 |
365 | Get users with companies
366 | User::with('companies')->get()
367 | ```sql
368 | SELECT * FROM users
369 | SELECT companies.*, company_user.user_id AS pivot_user_id, company_user.company_id AS pivot_company_id
370 | FROM companies
371 | INNER JOIN company_user ON companies.id = company_user.company_id
372 | WHERE company_user.user_id IN (1, 2, 3)
373 | ```
374 |
375 |
376 | Get users with employees (nested many to many relationships)
377 | User::with('companies.employees')->get()SELECT * FROM users
378 | ```sql
379 | SELECT companies.*, company_user.user_id AS pivot_user_id, company_user.company_id AS pivot_company_id
380 | FROM companies
381 | INNER JOIN company_user ON companies.id = company_user.company_id
382 | WHERE company_user.user_id IN (1, 2, 3)
383 | SELECT employees.*, company_employee.company_id AS pivot_company_id, company_employee.employee_id AS pivot_employee_id
384 | FROM employees
385 | INNER JOIN company_employee ON employees.id = company_employee.employee_id
386 | WHERE company_employee.company_id IN (7, 8, 9)
387 | ```
388 |
389 |
390 | Load companies for user
391 | $user->load('companies')
392 | ```sql
393 | SELECT companies.*, company_user.user_id AS pivot_user_id, company_user.company_id AS pivot_company_id
394 | FROM companies
395 | INNER JOIN company_user ON companies.id = company_user.company_id
396 | WHERE company_user.user_id IN (1)
397 | ```
398 |
399 |
400 | Load companies and employees for user (nested many to many relationships)
401 | $user->load('companies.employees')
402 | ```sql
403 | SELECT companies.*, company_user.user_id AS pivot_user_id, company_user.company_id AS pivot_company_id
404 | FROM companies
405 | INNER JOIN company_user ON companies.id = company_user.company_id
406 | WHERE company_user.user_id IN (2)
407 | SELECT employees.*, company_employee.company_id AS pivot_company_id, company_employee.employee_id AS pivot_employee_id
408 | FROM employees
409 | INNER JOIN company_employee ON employees.id = company_employee.employee_id
410 | WHERE company_employee.company_id IN (7, 8, 9)
411 | ```
412 |
413 |
414 | Get users who have companies
415 | User::has('companies')->get()
416 | ```sql
417 | SELECT * FROM users WHERE EXISTS
418 | (SELECT * FROM companies
419 | INNER JOIN company_user ON companies.id = company_user.company_id
420 | WHERE users.id = company_user.user_id)
421 | ```
422 |
423 |
424 | Get users who have companies with employees (nested many to many relationships)
425 | User::has('companies.employees')->get()
426 | ```sql
427 | SELECT * FROM users WHERE EXISTS
428 | (SELECT * FROM companies
429 | INNER JOIN company_user ON companies.id = company_user.company_id
430 | WHERE users.id = company_user.user_id AND EXISTS
431 | (SELECT * FROM employees
432 | INNER JOIN company_employee ON employees.id = company_employee.employee_id
433 | WHERE companies.id = company_employee.company_id))
434 | ```
435 |
436 |
437 | Get users who have companies with empty description
438 | ```php
439 | User::whereHas('companies', function ($q) {
440 | $q->whereNull('description');
441 | })->get()
442 | ```
443 | ```sql
444 | SELECT * FROM users WHERE EXISTS
445 | (SELECT * FROM companies
446 | INNER JOIN company_user ON companies.id = company_user.company_id
447 | WHERE users.id = company_user.user_id AND description IS NULL)
448 | ```
449 |
450 |
451 | Get users who have companies and load their companies
452 | User::has('orders')->with('orders')->get()
453 | ```sql
454 | SELECT * FROM users WHERE EXISTS
455 | (SELECT * FROM orders WHERE users.id = orders.user_id)
456 | SELECT * FROM orders WHERE orders.user_id IN (1, 2, 3)
457 | ```
458 |
459 |
460 |
461 |
462 | ## Aggregate functions
463 |
464 | `count`
465 | User::count()SELECT COUNT(*) AS aggregate FROM users
466 |
467 |
468 | `max` (`min`, `avg`, `sum`)
469 | User::max('age')SELECT MAX(age) AS aggregate FROM users
470 |
471 |
472 | `withSum` (`withAvg`, `withMin`, `withMax`, `withCount`)
473 | User::withSum('orders', 'total')->get();SELECT users.*, (SELECT SUM(orders.total) FROM orders WHERE users.id = orders.user_id) AS orders_sum_total FROM users
474 |
475 |
476 |
477 |
478 | ## Miscellaneous
479 |
480 | `latest` (`oldest``)
481 | User::latest()->get()SELECT * FROM users ORDER BY created_at DESC
482 |
483 |
484 | `withExists`
485 | User::withExists('orders')->get()SELECT users.*, EXISTS(SELECT * FROM orders WHERE users.id = orders.user_id) AS orders_exists FROM users
486 |
487 |
488 | `increment` (`decrement`)
489 | User::where('id', 2)->increment('bonus', 12)UPDATE users SET bonus = bonus + 12, users.updated_at = '2023-10-25 14:58:10' WHERE id = 2
490 |
--------------------------------------------------------------------------------