4.best Practise EloquentGitHub - Alexeymezenin - eloquent-SQL-reference - SQL Queries Generated by Eloquent ORM Reference
4.best Practise EloquentGitHub - Alexeymezenin - eloquent-SQL-reference - SQL Queries Generated by Eloquent ORM Reference
Star Notifications
README
Get
Where
Create
Update
Delete
One to One
One to Many
Many to Many
Aggregate functions
Miscellaneous
Get
get ( all )
User::get()
User::get()
User::withTrashed()->get()
SELECT * FROM users
first
User::first()
find
User::find(2)
findMany
User::findMany([1, 2, 3])
value
paginate
User::paginate()
Where
where
orWhere
whereIn
whereBetween
User::whereBetween('age', [20,40])->get()
whereDate
User::whereDate('created_at', now())->get()
Create
create
firstOrCreate
Update
UPDATE
UPDATE users SET name = 'John', email = '[email protected]', users.updated_at = '2023-10-04 16:12:30'
WHERE id = 1
updateOrCreate
Delete
delete
User::where('status', 'some_status')->delete()
User::where('status', 'some_status')->delete()
$user->delete()
$user->delete()
User::destroy(1, 2, 3)
User::destroy(1, 2, 3)
$user->restore()
One to One
profiles table has id , user_id , city
For nested relationships examples another one to one relationship is used. Profile hasOne passport, passport belongsTo profile
$user->profile or $user->profile()->first()
SELECT * FROM profiles WHERE profiles.user_id = 2 AND profiles.user_id IS NOT NULL LIMIT 1
User::with('profile')->find(2)
$user->profile->passport
Get user with profile AND passport (nested one to one relationships)
User::with('profile.passport')->find(2)
User::has('profile')->get()
User::has('profile.passport')->get()
User::has('profile')->with('profile')->get()
One to Many
orders table has id , user_id , comment
For nested relationships examples another one to many relationship is used. Order hasMany support tickets, support ticket belongsTo order.
Get user's orders
$user->orders or $user->orders()->get()
User::with('orders')->get()
Get users with orders and support tickets (nested one to many relationships)
User::with('orders.supportTickets')->get()
$user->load('orders')
User::has('orders')->get()
Get users who have support tickets (nested one to many relationships)
User::has('orders.supportTickets')->get()
User::has('orders')->with('orders')->get()
Many to Many
companies table has id , name
For nested relationships examples another many to many relationship is used. Company belongsToMany employees, employee
belongsToMany companies
$user->companies or user->companies()->get()
User::with('companies')->get()
User::with('companies.employees')->get()
$user->load('companies')
Load companies and employees for user (nested many to many relationships)
$user->load('companies.employees')
User::has('companies')->get()
Get users who have companies with employees (nested many to many relationships)
User::has('companies.employees')->get()
User::has('orders')->with('orders')->get()
Aggregate functions
count
User::count()
User::max('age')
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
Miscellaneous
latest (`oldest``)
User::latest()->get()
withExists
User::withExists('orders')->get()
SELECT users.*, EXISTS(SELECT * FROM orders WHERE users.id = orders.user_id) AS orders_exists FROM users
increment ( decrement )
User::where('id', 2)->increment('bonus', 12)
Releases
No releases published
Packages
No packages published
Contributors 2