
Martin Joo
@mmartin_joo
š„Level up your Eloquent game In this thread, you can read about 5 pretty useful but not so well-known Eloquent features such as: - whereBelongsTo - whereRelation - Default relationship values š§µKeep Reading
1/6 whereRelation You can use the whereRelation helper to filter records based on Eloquent relationships. It uses EXISTS in the background so itās an excellent choice if you donāt need data from the related record:
2/6 whereBelongsTo With the whereBelongsTo helper, your code reads like an English sentence. Just take a look at this:
3/6 oldestOfMany If you often need the oldest record from a related table, you can use the oldestOfMany relationship type. It feels a bit more ānativeā than writing a regular query. This will return the oldest paycheck (based on the smallest auto-increment ID) for an employee:
4/6 Default attribute values Did you know that you can give default values for model attributes? Check this out:
5/6 Default relationships Not only for attributes, but you can also give default values for relationships. Easy as usual:
6/6 Thank you, if you're still here! Just released a new book - Building a database engine Check it out here: https://thedatabasebook.com/