⚡️Speed up a
#Laravel
app by caching the entire response. You can use Spatie's ResponseCache package or create a middleware to store the response in the cache:
#PHP
Tip
Make use of Laravel's "dot" notation for accessing nested array values. 🎯 Simplify accessing multidimensional arrays with a single dot-separated string.
Laravel Tip
Do you want to keep a tab on your laravel application's queries?
Just add this to the boot() method in AppServiceProvider to get them in your logs:
1/3. Laravel Tip
Did you know... While iterating through a foreach loop, a $loop variable will be available inside of your loop. This variable provides access to some useful bits of information.
More info in the short 🧵👇
#PHP
Tip: Make use of Laravel's "dot" notation for accessing nested array values. 🎯 Simplify accessing multidimensional arrays with a single dot-separated string.
#Laravel
Tip: Optimize database performance by utilizing Laravel's query caching. By caching frequently executed queries, that are not meant to change for a while, you can reduce database load and speed up your application.
Laravel Tip
Careful when you call SUM, AVG or COUNT with Pagination!
- before paginate() - you get the SUM of the query;
- after paginate() - you get the SUM of that page;
#Laravel
Tip
Load data faster when the targeted value is an integer.
Use 𝘄𝗵𝗲𝗿𝗲𝗜𝗻𝘁𝗲𝗴𝗲𝗿𝗜𝗻𝗥𝗮𝘄() instead of the 𝘄𝗵𝗲𝗿𝗲𝗜𝗻() method to load data faster when the targeted value is an integer.
#Laravel
Tip: Use Laravel's "chunk" method when dealing with large datasets in Eloquent queries. It fetches records in smaller chunks, preventing memory issues and improving performance.
#Laravel
Tip: Load multiple levels of relationships with a single eager loading call. Use Laravel's with() method for eager loading nested relationships.
#Laravel
Tip
Do you want to keep a tab on your laravel application's queries?
Just add this to the boot() method in AppServiceProvider to get them in your logs:
#Laravel
Tip: You can add a URI fragment when redirecting to a route.
This is super useful when redirecting to a specific section of the page. E.g. reviews section on a product page 📈
Laravel Tip
⚡️Speed up a
#Laravel
app by caching the entire response. You can use Spatie's ResponseCache package or create a middleware to store the response in the cache:
#Laravel
Tip
Did you know about Auth::once()?
You can login user only for ONE REQUEST, using method Auth::once(). No sessions or cookies will be utilized. It's helpful when building a stateless API.
#PHP
Tip: Make use of Laravel's "dot" notation for accessing nested array values. 🎯 Simplify accessing multidimensional arrays with a single dot-separated string.
#Laravel
Tip: Load data faster when the targeted value is an integer.
Use 𝘄𝗵𝗲𝗿𝗲𝗜𝗻𝘁𝗲𝗴𝗲𝗿𝗜𝗻𝗥𝗮𝘄() instead of the 𝘄𝗵𝗲𝗿𝗲𝗜𝗻() method to load data faster when the targeted value is an integer.
#Laravel
Tip: The eloquent's method `orderByRaw()` let's us simply pass a raw query to order the results.
For example, sorting by the difference between two columns.
Laravel Tip
Do you want to test email contents in your app but unable/unwilling to set up something like Mailgun?
Just use the .env parameter MAIL_DRIVER=log and all the emails will be saved into storage/logs/laravel.log file, instead of being sent.
Laravel Tip
Did you know about Auth::once()?
You can login user only for ONE REQUEST, using method Auth::once(). No sessions or cookies will be utilized. It's helpful when building a stateless API.
#PHP
Tip
Make use of Laravel's "dot" notation for accessing nested array values. 🎯 Simplify accessing multidimensional arrays with a single dot-separated string.
#PHP
Tip
Did you know about "First-class callable syntax"?
Using the first-class callable syntax, you simply call the function or method followed by an ellipsis (...) to create a first-class callable. That way, you can rewrite that code like this:
Laravel Tip
Did you know that you can re-use an existing Laravel Eloquent scope in defining a new relationship, instead of writing the conditions all over again?
Laravel Tip
Did you ever need to generate temporary URLs for file downloads in
#Laravel
?
Use the temporaryUrl() method to generate a signed temporary URL valid for the specified duration:
#Laravel
Tip: Want to use UUID as primary keys in your Laravel models? 🆔 Simply set the 'uuid' attribute as the primary key and the 'string' as the key type. Laravel handles UUID generation automatically! 😍
#Laravel
Tip
The eloquent's method `orderByRaw()` lets us simply pass a raw query to order the results.
For example, sorting by the difference between two columns.
Laravel Tip
Do you want to know how your scheduled task performed at midnight?
You are in luck! You can easily save or email the output of your scheduled tasks:
#Laravel
Tip
Be careful when you call SUM, AVG, or COUNT with Pagination!
- before paginate() - you get the SUM of the query;
- after paginate() - you get the SUM of that page;
#Laravel
Tip: Enhance validation by creating custom validation rules in Laravel. You can define your own rules to handle complex validation scenarios, specific to your application.
1/3.
#Laravel
Tip
Did you know... While iterating through a foreach loop, a $loop variable will be available inside your loop. This variable provides access to some useful bits of information.
More info in the short 🧵👇
#Laravel
Tip: Simplify eager loading with Laravel's withDefault() method. 🔍 Load relationships and provide default values if the related model does not exist.
#Laravel
tip: You can create custom disks at runtime without the need to have the config in your config/filesystems file.
This can be handy to manage files in custom paths.
#Laravel
Tip
Sometimes, you may want to prevent saving or updating on some condition or stop some users from updating records.
Here is a simple trick to prevent saving or updating via model events:
Laravel Tip
We often convert values while displaying in views or taking inputs in controllers before saving!
Better, we define `Accessor` & `Mutator` in our model once. It converts the attribute every time before saving or retrieving it.
Quick example:
Laravel Tip
Did you know about Boot Eloquent Traits?
For example, if you have multiple models with slug, you don't want to rewrite the slug creation logic in every model. You can define a trait and use a boot* method:
#Laravel
Tip: Use Laravel's optional() helper with a callback to handle null values gracefully. 🕶️ That way, you can perform operations on potentially null values without throwing errors.
Laravel Tip
Did you want to do something when your scheduled tasks either succeeded or failed?
You can use the `onSuccess()` or `onFailure()` callbacks to take any action you want!