Laravel Community Tools by Tighten

Category "Eloquent" tricks

140 tricks
Add New Colum To Exist Table With Seeder
faizalprib... 11 years ago 8845
4.0
A variation of http://www.laravel-tricks.com/tricks/column-selection-in-eager-loading Credits: http://stackoverflow.com/questions/16994253/laravel-eager-loading-load-only-specific-columns Warning: this trick does not work on many-to-many relations, see: https://github.com/laravel/framework/issues/2966
orlissenbe... 11 years ago 18877
Sometimes you need to add a new column to a table that already exists, for this we can use laravel migrations. In our command line we type (You can name the migration file as you want ) : php artisan migrate: make add_field_to_table. In line 14 we're adding a new field (table column) into our table. We type the field type if you don't know the field types you can find them in the documentation: http://laravel.com/docs/4.2/schema. Continuing with, we can specified if we want to add the field before or after an a column that already exists of course. In this case i'm using after but you should use whatever you need. Once we have finish, we go back to the command line we type: php artisan migrate. And that's it, we've updated our table. I guess that we can add as many fields as we need, i didn't try that but i think that it will works too. Cheers!
selenearzo... 11 years ago 31709
Reference the connection by name, the database could be named differently in another environment.\ Note: you might want to add backticks to the database name if the name uses alternative characters.
orlissenbe... 11 years ago 12506
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 27729
Sometimes you'll need to put custom fields in your dropdown field, like user's firstname and name. That's how you can do it.
the-juju 11 years ago 13411
//laravel 5 make migration
doguhan 11 years ago 16847
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 19849