Do you know what is Accessors and how to use them to add custom columns to laravel models? Let’s see in this tricks.
What is Accessors in Laravel?
The Accessors in laravel are custom methods defined by the user. You have to create a custom method in Models. Accessors are used to format the columns/attributes when you are fetching from the database.
Using Accessors in Laravel
By using this, we can combine the multiple table columns in one attribute or any kind of customization with data like uppercase, lowercase, string search/replace, etc. And you can use it as a property.
Let’s see the following code.
In the above example, we made an Accessors function to get the full name of the user by combining the two columns’ firstname and lastname.
After making the Accessors in laravel models, you can use it as the following code.
Hope you like the information!
Also Read: How to Install Laravel Framework on Windows and macOS?
Also Read: How to Use Where Condition in Laravel Eloquent?
Being Tricky 😉