Laravel Pro Tips
Here's a pro tip 🔥 for all you Laravel developers: You can customize the default order of data retrieved from relationships!
#Laravel #PHP #PHPRC
Mohammad Haris ⚡
🔥 #Laravel Tip: Eloquent offers many undocumented methods that can help to make your code easier to read. For example, the whereBelongsTo method simplifies finding resources that belong to a model instance.
🔥 #Laravel Tip: Eager loading model relationships is way more efficient, however if used blindly, it can result in the fetching of data that you don’t actually need. To help avoid this problem, consider adding a constraint to the query…
#Laravel tip: Find Related IDs on a BelongsToMany Relationship with using allRelatedIds() method 😎
Laravel Tip 💡 v9.16 introduces a new withWhereHas() to reduce code duplication 💅 Time to do a bit of cleaning 🧹
When eager loading relationships in Laravel, you can specify the columns you need directly in the `with` method 🍃
An efficient way of checking if a relation exists in Laravel is to use withExists.
It performs a simple existence check and returns a boolean.
By default it would return a "posts_exists" value on the User.
You may alias it like in the example below to make it more readable.
🌶️ #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 #Database Tip:
While creating migrations you may also specify the desired action for the "on delete" and "on update" properties of the constraint.
Here are more methods that #Laravel provides 😎
#Laravel #Database #PHP https://t.co/pr3arpPGdk
May 2nd, 2022
The new findOr method in Laravel 9.x
A minor release for Laravel 9.x dropped a few days ago and among lots of new features and improvements, this release has introduced a new method called findOr in the Eloquent Builder and Relations.