Laravel Community Tools by Tighten

Mei Gwilym

Joined: 11 years ago
Total tricks: 3
Last trick: 11 years ago

Submitted 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 11300
You have election data in your database. Perhaps each candidate has 1 record, with his/her name and number of votes. The percentage of the vote that each candidate attained can be calculated per candidate, without having to loop through the data. Laravel's Collection class provides the `sum($column_name)` method, which sums the column totals. This can then be used to calculate the vote share. I've used PHP's `format_number` function to round up the final percentage. Extra points given for a bootstrap progress bar, to illustrate the vote share.
meigwilym 11 years ago 19800
Often a foreign key needs to be set to NULL. On my projects I often use a drop down for this, and using an empty string as an option value does not work. I (hopefully) improved on this method[1] to set null values only on certain fields when saving a model. The original idea is from stackoverflow[2]. This method uses a BaseModel from which all models inherit. Any FK fields should be added to the model's $nullable property (an array). The work is done by the BaseModel. [1] http://laravelsnippets.com/snippets/set-fields-to-null-instead-of-empty-value-to-avoid-problems-with-nullable-foreign-keys [2] http://stackoverflow.com/questions/17452923/empty-string-instead-of-null-values-eloquent/19101473#19101473
meigwilym 11 years ago 27692