Laravel 10 full course for beginner - accessor mutator

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now we have to understand more about the power of eloquent models right now I have deleted all the users from the database let's try to create them one by one using three different methods we have so one is this one where sarthak1 second is query Builder I can say sarthak 2 and third is going to be the user model with sarthak 3 as the emails okay and then I will reload it it has done something and then I'm going to comment everything and then fetch the users and see we have three different users very good now if I open the first one and see that the password is just a plain text password it should be encrypted one this is obviously with the raw SQL query so we don't have any expectation what about the query Builder if I do that query Builder is having the plain text this is not expected with this what about the user model user model still is not encrypting the password so how do we make sure that whenever you use the model the eloquent model you can encrypt the password so this is one thing is we can just do this we can say hey the fourth one and we have to use a very nice function or a global function called decrypt be Crypt okay so this is a function given by laravel which actually hash the value or the string we provide and convert it into a hashed password now let's try to do this and see what's going to happen reload then comment it and once again check the latest one and this time you can see the password is actually encrypted but this is all good but we don't want every time to decrypt the password like this what I want is if user is giving just the plain text then automatically it will be decrypted or hashed into the database for that the power of eloquent user model will come so how we can use this elegant model to do this automation thing we call it mutation now if we go on the database documentation of laravel go inside the eloquent orm we have this mutator slash casts so we are interested in the mutated mutation so if you scroll down you can see that there is a mutation thing so how we create a mutation you have to create a protected function with the same name as the field name and then the set part is the mutation the get part is the accessor we will talk about this accessor very soon but for now we just need the set part so how do we work on this for our password thing go to the user model at the bottom create a protected function and I'm going to call this function as pass word obviously this function has to return attribute so you have to type in it then you need to return attribute and here then make inside that as we know we have to create a set and using the arrow function we get the value now what we need to do with this value we always need to do the decrypt for this value that's it so we have successfully created one mutator this means whenever we use the eloquent model to create a new user the password will automatically be decrypted and then stored into the database so let's try to create another user like this one and I'm going to actually use the user model and say sarthak 5 or maybe six and I need to do the user here and try so you can see we have the user but this time password is encrypted although we are not using decrypt here the password is still encrypted I just want to make sure this is working so I just comment this one and change the email field and try to create another user once again and see what's going to happen and see the password is not encrypted this means this function is really responsible for first decrypting the password or hashing the password and then storing into the database so this is the power of our mutator and power of the eloquent user model now next is the accessor so accessor is just the reverse of a mutator so what mutator do mutator first change the field and then store it into the database accessor is reverse accessor will first get the field and then change something okay so what I'm going to do I'm going to fetch a single user so here let's say I want to get a user with the ID of what's the id 17. okay so let's use the ID 17 this is the user we have so call it user not users so we have the user reload and now what about I want to get the name so right now if I see here the name field is as it is which is inside the database but what if I want to upper case all the user name so how do we do that similar to mutator we need to create one get field here which is called accessor so it's going to be super easy here I go and instead of password I create another protected function but this name the name is the name field because we are going to interested in the getter or in the accessor of name field so now change it to get and I just want to say Str to Upper okay so this is going to make things uppercase when you fetch the field not just directly from the database but first we get from the database and then you call for single field okay so let's go reload and boom you can see it is all uppercased isn't it really amazing to have the accessor and the mutator inside our laravel eloquent and that's the power of alignment one more bonus tip is here since laravel is making life of developers easy it can also provide the Str class so you have to import it at the top you say use Str and it should always be from the illuminate support Str and once you do that you say colon colon upper this is the same thing doing str2 upper with str2per is the core PHP thing Str colon upper this Str class is very useful given by larval and that is the same thing okay now if you want to learn more about the Str thing so just search for Str and colon colon upper and here we are the helpers are here and there are a lot more amazing helpers available here if I scroll up at the top you can see array helpers are there but we have created we have used a string helper so string helpers are here which is like this okay so I think you got the point like how we can use the accessor how we can use the mutator and what is the difference between accessor and mutator okay so that's really cool let's move to the next topic
Info
Channel: Bitfumes
Views: 12,995
Rating: undefined out of 5
Keywords: Laravel 10 for beginners, Learn Laravel 10 from scratch, Laravel 10 course for beginners, PHP framework tutorial, Laravel MVC tutorial, Blade templates tutorial, Laravel routing tutorial, Eloquent ORM tutorial, Laravel database tutorial, Laravel forms tutorial, Laravel authentication tutorial, Laravel authorization tutorial, Laravel validation tutorial, Laravel CRUD tutorial, Laravel REST API tutorial, RESTful API tutorial, API development tutorial, laravel routes
Id: aGZ9vYCbW1U
Channel Id: undefined
Length: 9min 27sec (567 seconds)
Published: Mon Mar 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.