Hey Laravel Developers
Starting with Laravel v11.38.0, we can now customize the validation rules for "Email" similarly to how we have them with the "Password" rule using fluent method chaining.
This approach of chaining on methods over using strings leads to less typos and also brings more opportunity for customization in the future like configuration of defaults.
Here is an example of before and after you use the fluent Email class for validation.
Hey Laravel Developers
When defining relationships on our models, we often follow the convention of naming the relationship similar to the related table name.
For example, if we have a "Car" model, we define the "user_id" column on the "cars" table while defining the "users" relationship, which represents the owner relation.
However, I prefer naming columns based on the domain for which I am building the application.
For instance, in this scenario, instead of naming the column "user_id", I would name it "owner_id" and define it as the "owner" relationship.
To me, this approach feels more fluent and readable.
What do you think? Let me know your thoughts!
So it depends on whether you aim for making the db clear or the code clear.
Thats a tradeoff you have to decide for.
Hey Laravel Developers
You might have used the exists() method on the Query Builder class to check for the existence of a record in the database.
But did you know we also have the existsOr() and doesntExistOr() methods available?
These methods accept a callback function as an argument, which is triggered based on the existence or non-existence of the record.
To me, it feels like a more convenient and readable way to write code.
Check out the example below!
Hey Laravel Developers
Let me introduce you to a new package that I encountered recently which is json-collection-parser by maxakawizard
So this package allows us to parse large JSON files and process them in chunks instead of reading and keeping the whole file in memory.
This keeps the overall application memory footprint low while working with large datasets.
Have you used some other package for a similar kind of scenario? Please let me know what you think.
Hey Laravel Developers
Laravel provides us with the belongsToMany() relationship, allowing us to form many-to-many relationships between two different models in our application.
An example of this relationship is the User and Role model where one Role can belong to many users and one user can belong to many roles.
We create an intermediate/pivot table to map the relationships between these two models.
But do you know that you can also maintain timestamps for when these relationships were formed, by using the withTimestamp() method while creating the relationship?