Laravel Pro Tips
🌟 Eloquent tip
If you need to insert or update a whole bunch of records in a loop, don't use `Model::updateOrCreate`.
Instead, use the `DB` facade's `upsert` method to use your database's built-in upsert functionality to do the whole thing in one query!
Kai Sassnowski 🥔
🔥 #Laravel Tip: Knowing the difference between calling ‘fresh’ or ‘refresh’ on a model is quite important. If you’re new to the framework, then here’s a simple explanation on how they differ.
Laravel tip 💡You can use the 'resolveRouteBinding' method on your model to modify the value that is used to resolve the binding.
🔥 #Laravel Tip: At scale, calculating metrics for dashboards can take a long time, as you might have to count thousands or even millions of records. Fortunately, you can easily fix this by using model events to maintain stats. Here’s a simple example:
🌶️ #Laravel Tip: The Eloquent push() method allows you to save the Model and all of its relations in one shot. Compared to the save() that only saves the Model itself.
Here's a practical example to illustrate 🔥
Bilal Haidar - Available 4 Laravel gigs
🔥 Laravel Tip: Factory states
You can create factory states in your model factories to hide the details about the model attributes and make your tests more readable.
#Laravel Tip 🔥 When using a custom keyed implicit binding as a nested route parameter, Laravel will automatically scope the query to retrieve the nested model by its parent using conventions to guess the relationship name on the parent.
#Laravel Eloquent Tip 🔥
You can use 'save' and 'saveMany' methods for adding new models to the relationships.
#Laravel Tip💡Put this trait on all your models to track easily who created and updated a model.
No articles were found.