
𝗧𝗶𝗽 𝗼𝗳 𝗧𝗵𝗲 𝗗𝗮𝘆 𝗳𝗼𝗿 𝗟𝗮𝗿𝗮𝘃𝗲𝗹𝗶𝗮𝗻𝘀❤️
Hey! Do you know that we can change Model attributes on the fly in #Laravel?
With 𝗔𝗰𝗰𝗲𝘀𝘀𝗼𝗿𝘀 𝗮𝗻𝗱 𝗠𝘂𝘁𝗮𝘁𝗼𝗿𝘀 you can modify Model attribute before you get/set them. It increases code maintainability & reduces the need for extra code.
𝟏. 𝐖𝐡𝐚𝐭 𝐢𝐬 𝐇𝐚𝐩𝐩𝐞𝐧𝐢𝐧𝐠?
𝗳𝗶𝗿𝘀𝘁𝗡𝗮𝗺𝗲(): A camelCase function representing your Model attribute/database column which you want to modify e.g
attribute first_name === firstName()
𝗴𝗲𝘁: An accessor that transforms an Eloquent attribute value when it is accessed.
𝘀𝗲𝘁: A mutator that transforms an Eloquent attribute value when it is set.
𝟐. 𝐔𝐬𝐞 𝐂𝐚𝐬𝐞
If you want to format data automatically, such as capitalizing names, converting enums into representable values or hashing passwords, #Laravel Accessors and Mutators are there to help you on the fly.