HasMany to HasOne
Hey fellow developers! Laravel 10 has been around for a while now, and it comes with a range of neat features that have certainly improved our coding experience. Besides the headliners like native types, Laravel Pennant package, Process Interaction layer, and improved test profiling (which you can find on Laravel’s website), some subtle changes can provide a boost to our quality of development. Let’s dive in.
One of my personal favorites is the ability to convert an existing HasMany to HasOne relationship. Check out the code snippet below:
If you’re like us at Pionect and have been relying on the eloquent-has-many-deep package primarily for this feature, you can now part ways with it. The result? Cleaner relationships and less dependency clutter!
Eloquent large queries with Laravel 10
Another improvement that Laravel 10 brings is directly related to our work with large queries in Eloquent. We’ve all been there, wrestling with a complex query, copying and pasting bindings manually into a query to run it in a database GUI. But that’s a thing of the past, thanks to the toRawSql() method. This handy little feature dumps the query, including the bindings! It’s a small change, but one that will save a ton of time and frustration.
isHashed password handling in Laravel 10
The handling of passwords has also been improved. The new isHashed function, used like Hash::isHashed($value), simplifies your code by eliminating checks using password_get_info. Additionally, the hashed cast, used as public $casts = [‘password’ => ‘hashed’]; ensures attributes are hashed before saving. Both contribute to cleaner, more manageable code.
Laravel 10 offers great improvements, making coding easier and more efficient. Give it a shot. Happy coding!
Daniel Ducro
CTO Pionect
19 September, 2023