Laravel Basics - Accessors, Mutators and Casting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to lara tips so in today's video we'll be talking about eloquent mutators the eloquent mutator contains accessor mutators and attribute casting all these things and we'll be talking about all these things in this in today's video so if you haven't watched my previous video regarding model then please watch that so you will need that in this today's video so basically the accessor accessory and mutator mutator allows us to format the eloquent attribute values when we retrieve or set them on a modal instance yeah so whenever we are trying to access any value from modal instance then we can use accessor to modify that value and whenever we are trying to set or set any value in the eloquent instance and trying to save it in the database then we can change that value automatically using the mutator so let's see that in action so i have already created a modal product and a migration file so if you don't know how to create this then please see my previous tutorial i have already taught it there okay basically the product device contains here the titles log price and is active fields and the title and strings are sorry title and slogari string once the price is the unsigned integer which should always be greater than zero so it is unsigned so boolean means tiny integer which is is active so it can only be zero and one so i have created it as a boolean so now what we want is whatever be the title of the product we always want to see the first letter of every word as a capital letter so for that let us save one data in a database let us go to home controller so how to say we have seen this in our previous tutorial okay now let's save one product in our table so i have added this data title is the samsung galaxy s20 so all our small letters and the slog is just str slog of this title so this is the helper str helper class which is provided us by the laravel and it is imported here illuminate support str and what it does is it basically basically converts this string into a slot which is all the small letters and separating each is replacing each space with dash and we having thought of price and is active as a true and now let us save this okay so if i go to the home page and refresh the page so it should run this execute this method and our data should be here in our database so you can see here samsung galaxy s20 and all those data are being saved here now we want to access this data let's see we know that how to access access this data yeah so it basically it has id of one so let us find let us return the product find the product with the id of one and let us return this and if i go to the home page and refresh the page you can see here it is giving us the same title but we don't want this title we want to show every first letter of the word as the capital letter so what we can do is we can do like this product equals to this yeah then title is equals to str title product title and then return product yeah so this hdr title this is also helper method in this hdr class so which converts every first letter of a word into capital letter so now we have returned it here now let's see what is the result now you can see here every first letter of this word is now capital letter yeah so we have done it like this so and everywhere everywhere we want this feature then we need to find a product and again product arrow title and give it a title like this yeah and return a product like this so this is a little bit cumbersome so now we want this thing to happen automatically so whenever we access product title it should automatically convert this title into the uppercase words okay so how can we do this we can do this by using accessor now let me just remove this okay and let me go to the models app models and product yeah here now you can see here in the documentation you can see here define an accessor here you can see we can define an accessor like this here so let me copy this and i'll explain you this so let me paste it here so every accessor will start with get okay and it will end with attribute okay every accessor will start with the gate and it will end with attribute and in the middle we need to give the name of the database table volume yeah for now what we want to do is we want to convert this title into the uppercase words yeah so now we have to give a title title here okay get title attribute and if we want this for the slog then we can give here get slog attribute yeah and there is let's see if there is a is underscore active column then what we can do is we can do it it is like this in the cameo case is active we can define it is in in a camel case and it will automatically take this is underscore active okay this is by convention so we need to follow this convention so for now let us write title and and this value okay this value is the this value will get here okay now what we can do is str title let us import this title it is imported here and if i pass value here now whenever we access title from the product model then it will automatically convert this into the title case okay now let me show you here okay we have found a product and now return here now it should automatically convert it into the title case now let me go here now and refresh the page here now you can see here it is automatically converted it into the title case so just to confirm let me comment this and save it here and if i refresh here now you can see here it is again small letter and if i uncomment it save it and refresh the page then it you can see here it is automatically converted into the title yes so this is the accessor so now let me show you the mutator so mutator is used to modify the data whenever we assign a value to the model instance okay okay now let's say we we want to save something to the database now let's do this for the price basically we always store price in the sense yeah because of the some floating point issues so there here is a good article about this i'll link this in the description below the like button so you can read about this it is just a small article so you can easily read and understand about this okay so we we should always store that the price as a integer value and in a sense we should never store it as a floating point value okay whenever a price is 1000 then we we want to add it two more zeros to it okay so how can we do it so i'll show you how we would do without mutator first and then we will again use rotator okay let me undo the changes that i have done yeah now let us give another product name let's say iphone x okay now it is its price is let's say 1200 yeah and whenever we want to save it as a send then what we can do is multiply it by 100 so it will be converted into sent okay and if i go to the home page and refresh the page here yeah and go to the our database table and refresh here now you can see here we can see our iphone x and the price is multiplied by 100 and it is stored in the database so wherever you want to store in different parts of our project then everywhere you have to remember that you need to multiply it by 100 so we don't want that we want this to happen automatically yeah so now what we can do is we can define a mutator for this similar to that of the accessor we have mutator so let's go to the documentation and we can see here mutator okay so if i go here and the the convention is similar so before we had the get title attribute now what we can do is get price attribute set price attribute okay the set will be in the beginning attribute will be in the end and this will be the name of the table column name yeah and this value okay this value will be this value so we don't want this thing we only want to save it like this just price so this will be this 1200 only yeah this price will be received here now what we can do is here this attribute price value into 100 so if we do this and now if we save let's say 900 and let's say one plus i don't know what's the latest one plus one so i'll only write one plus here so whenever we save here okay the whenever the price we save the price so 900 should be converted into so it should be multiplied by 100 and it will be automatically converted into the 90 000. so we haven't done anything yet so now we don't need to remember we need to multiply the price field by 100 always so we don't need to remember that now if i go to the home page and refresh the page now that should be 90 000 here now you can see here it is 90 000 here so this is how you use mutators now let us look at the another feature which is attribute casting yeah so let me again show you one thing here let us find the product with the id one yeah now you can see here this is activities one we have defined in the migration as a boolean value but in my sql database there is no boolean value so we have to store it as one or zero so we have stored it in a tiny integer and if we go to the browser and in the home page if we refresh here now you can see here is active is one but we don't want we don't want this to be one yeah so for this also what we can do is we can define accessor so let's do that and i'll show you another way of doing that as well if we go here and just do it like this and get is is active attribute and return value is equal to 1 yeah if we do it like this then whenever the value is 1 it will return true and whenever the value is 0 it will return false now if i go to the browser and look at here it is currently 1 and if i repress the page now you can see here it is automatically converted into a boolean value okay so instead of doing this what we can do is there is a cast property protected property in laravel model so cast and what we can do is is active to boolean yeah so this is basically saying cast is active to boolean okay now again if i come here and refresh the page now you can see here it is again converted into true and just to confirm i'll comment this and refresh the page and now you can see here it is now an integer value and let us keep it like this and see here so you can see it here as well yeah it is showing here there are a lot of casting feature in laravel so we can use it like in in place of boolean so for the different kind of values we can use different kind of casting yeah we can cast the value to either integer float real double this string boolean object or array yeah or into the daytime format or as well as the larval collection you can try some of these and just play around with it and see what are the results whenever you use some of this casting die okay okay currently we have only a products table with the price field now let's say we also have other tables and that also have price field in it and again in that model we don't want to repeat this code yeah because we have to follow the dry dry do not repeat yourself principle so we don't want to repeat our code everywhere so whenever we want to change in one thing then we have to again com go to the every places and again changing the every model so we don't want that so for that laravel also has one feature called custom cast yeah we can create custom cast and use that in the model in every model wherever we want so for that let us create money cast if i go to here so for this we don't have any artisan command so we have to create it by ourself so inside the app let me create the casts casts folder and moneycast.php and let us copy this everything from here and let us paste it here i'll explain you this so okay it is inside the app app cast name space so it's this one is correct so this one is also called and this cast attribute so let us rename this as money cast because this is our class name and it should always implement this cast attribute so if we go inside this and look so it is basically an interface which has two methods get and set so this money cast has two methods get and set so this gate method will be will act as accessor and this set method will act as mutator so if i let me just remove this okay now let me go to the go here and the set price okay so for that let me keep it as well so let me go here here and just what we need to do is return value into 100 value yeah this value is this value so this is this value will come from that whenever we try to save any integer value to the price field okay so now this is the integer and this model is the modal instance so let's say the product model instance that we are going to save so it will be received here the key is so we are trying to save in the price field so it will be this key will be price and value will be the whatever value that we are saving so let's say we are saving 900 dollar then it will be 900 here and attributes will be the array form of modal attributes we have this title slog and whatever we are trying to save in the database so all this will be passed here as an array so while saving the price we want to multiply it by 100 to make it set but when receiving the value while displaying it or while using it we want it to be in dollar yeah so what we can do is value divided by 100 now whenever we access price so it will always be in dollar so currently what is happening here here is in the home controller we are written in returning the product and if we go here and you can see here so it's 1000 so it's 1000 here yeah so for the one plus the id is three and you refresh here so it's showing us ninety thousand but the price is only nine hundred dollar yeah so it should only show 900 here okay for that so this will convert that into 900 okay now how to use this cast here moneycast so basically we'll go to the product model and inside the cast we'll give the key price and we'll give here moneycast class so this moneycast i have already imported it here then now we don't need this accessor over here now this casting moneycast will perform both accessor and mutator now if we go here in the home controller now we are again returning the product yeah now the value this 90 000 must be changed to 900 yeah now you can see here the cast is being applied and just to confirm i'll just comment it comment this out and refresh the base and again it is converted into 90 000. so now in any other model if you want this money cast then we can simply define a protected cast property and inside that we can give it like this so we don't we don't we are not repeating r and this dividing and multiplying logic in every modal so this is a very simple example so as i said in the beginning of the video we can do this to encrypt certain data while saving it to the database and while getting it we can automatically decrypt it and get it yeah we don't have to just use it in the every places just to define it in it in the cast and we it will automatically be converted so similarly you can do this for the title attribute as well so you can make one title cast dot psp and you can define the get and set accordingly in the last i'll be just showing you this array and json casting okay suppose you have a field json field in your database table column and whenever accessing the value you always want it to be array so what you can do is you can cast that column json column as an array and whenever you try to access let's say there is an option like this options column which is a json and whenever you say let's say product arrow options then if the json value will automatically converted into the array and this array is present in the default label code base okay so we can just use it we don't need to define anything so that's it for this today guys so in the next video we'll be talking about eloquent scopes how can we extract some logic into this scope and use it and make our controller clean thank you for watching have a great day bye
Info
Channel: Laratips
Views: 8,166
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel 8 tutorial, laravel 8, laravel 8 basics, laravel basics, laravel mutators, laravel accessors, accessor and mutators, laravel casting, laravel custom cast, laratips
Id: rie3Ot7C1Ek
Channel Id: undefined
Length: 20min 30sec (1230 seconds)
Published: Thu Oct 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.