Laravel Community Tools by Tighten

Tag "database" tricks

87 tricks
This trait allows you to save any column from any model into a location within the storage directory to allow the column to be blade compilable with view()->file($model->getViewableColumnPath('column_name')); The trait
leemason 10 years ago 6862
Model Based Setting Different Connection when working with 2 or more database in laravel
dhrubo001 10 years ago 6844
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 18860
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 31704
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 12501
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 17572
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 20319
You can find it here as well: https://github.com/mousavian/laravel-4-custom-db-log or you can just simply do: 1. create logger.php in your "app" folder 2. edit "app\start\global.php" file (or local.php), and add this: 3. all logs will be at "app\storage\logs\query.log"
mousavian 11 years ago 23120