Laravel Tips (@Laravel_Tips) header
Laravel Tips (@Laravel_Tips) profile picture

Almost daily Laravel tips from @crbdev

Joined September 2016
Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)Photo by Laravel Tips (@Laravel_Tips)
Laravel Tips retweeted
Be Smart, get smarter nomadphp.com/be-smart-get-sm…
64
1,941
102
483
I often see DB::raw being used for subselects, but you can do them in query builder using a closure as the 2nd argument on the where clause.
5
19
Need multiple paginators? Just pass a name as the 3rd argument of paginate method. This gets used in querystring. 2nd param is for select
3
In blade {{$collection->total()}} displays number of records in the whole collection {{$collection->count()}} only gives paginated amount.
1
4
When defining relationships, add ->select to slim down what's selected from the relevant table, otherwise Laravel will default to SELECT *
5
16
Mysql generated columns allow you to create columns based on your data and complex sql expressions, as always laravel as you covered.
5
8
When deploying you can speed up your app by running 'php artisan optimize' as a post deploy step.
In a migration, if you want to start incrementing from a number other than 1 use DB::update('ALTER TABLE tablename AUTO_INCREMENT = 1000');
6
11
Watch out for SQL injection issues when using DB::raw, use ? or :named placeholders, One gotcha! Variables are argument on select not raw
1
5
If you want to union more than 2 queries in query builder, fluently chain multiple ->union() calls in the final part of your query.
1
10
22
You can create custom messages on your own formRequest objects by adding a messages() method.
6
15
Want to get 3 random rows from a table try $RandomProducts = Products::orderByRaw('RAND()')->take(3)->get();
To edit the default Blade files for password resets etc, php artisan vendor:publish will copy the relevant templates to your resources dir
When using whereMonth in an eloquent query, you probably want to also use whereYear otherwise data from all years will be returned
1
Reset all migrations and reseed your database with one artisan command php artisan migrate:refresh --seed