Laravel Community Tools by Tighten

Category "Eloquent" tricks

140 tricks
Laravel's docs suggest to override the Model's getDates() methods when adding your own date fields. http://laravel.com/docs/4.2/eloquent#date-mutators To keep the default created_at, updated_at and deleted_at date fields, these must also be returned by the method, giving you at least 4 elements to the array. An easier (or at least, tidier) way is to set the protected $dates property with your own fields. These are merged with the defaults.
meigwilym 11 years ago 11328
Since Eloquent doesn't do a real join when using "with" this will add a simple join method without having to worry about the join columns since it's all stored with the models anyway. It will also select all the columns from the joined table with an added prefix of the table name.
phazei 11 years ago 45269
If we have a blog, sometimes we want to display all latest posts by category. Take an example with this website where I put those posts in a carousel on the homepage: http://dadack.com
mercuryser... 11 years ago 17579
This scope function applies a callback to the elements of a given query collection.
jplozano 11 years ago 9186
You can chain eager loading to avoid N+1 problem. Suppose you have 3 tables. customer[id, email], pos[id, serial_number] and restaurant[id, name, customer_id, pos_id] so restaurant table can act like a pivot table and it also have some value attribute. So now we can call customer from pos table via chaining eager loading.
milon 11 years ago 20324
Laravel has many choices for performing a query, sometimes it could be confusing, I hope this list help you to clear any doubts. If you know other way please show it in comments for add it.
luismec90 11 years ago 13951
Change the created_at, updated_at and deleted_at column names (deleted_at not working @ Laravel 4.2)
seriousfre... 11 years ago 15929
4.1
‍ക്ക് പരിക്കേറ്റിട്ടുണ്ട്. മരണസംഖ
Bribin 11 years ago 0
With this trick you will able to unitTest complex query, that will have closures.
fenos 11 years ago 15286