Laravel Eloquent Tricks and Tips
Here is my list of tips, tricks and short articles for doing related tasks in Laravel.
- A list of operators on Eloquent's where() method
- How to check if a relation was loaded on an Eloquent model already?
- How to eager load multiple levels of relationships in Eloquent?
- How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?
- How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?
- How to get table column names from a database table in Eloquent?
- How to get the first row that matches some where queries, or create it if it doesn't exist (in Laravel's Eloquent)?
- How to order a Laravel hasMany relationship?
- How to prevent Eloquent from adding created_at or updated_at timestamps?
- How to provide a default model object for relationships?
- How to return an item in Eloquent, or throw a ModelNotFoundException exception if it isn't found?
- How to select all rows for a certain month (or day, year or time), using Eloquent
- How to undelete a soft deleted Eloquent row?
Laravel Eloquent Cheatsheet (all on one page)