Laravel Eloquent Model Tutorial with example - Complete Guide on Eloquent Model in Laravel 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to see about laral eloquent model so guys we are going to cover what are the things for what we are using this eloquent model so guys let us understand first what is the eloquent model in larl so a larel model represents and interacts with the database table in the application okay so this model eloquent model interacts with your database tables okay so guys now let's get started so first we move to our editor so now guys uh to create a model or eloquent model in LEL there is a Artis command so let us type that PHP artisan make colon model and the model name so model name I'm going to keep as product so product and why I am keeping this product guys because I have already created the migration for this so if you don't know how to create a migration and don't understand what is migration in L please watch out the previous video link given in the description okay so guys let me just show you my migration file about products so here is my products table okay so guys now let me close it and now let's hit enter so I have a table now I am going to create a model so as here you see our model is created successfully so which is inside the app model and product.php so let's go inside the app folder models and product. PHP perfect and now we are going to interact with our database tables so how can you specify this uh product.php is the exact table name so let me show you my table here so inside my larel tutorial I have a named table called products okay so this products name we have to tell Here and Now guys let us declare the table name here so let's tell protected dollar table property equal to inside single codes or double codes you can use anyhow and give your table name so my table name either you can copy directly from your database whatever you have declared here and paste it okay and now guys let us see with the fillables so basically fillable property what does so it's nothing but it callings your column names okay so all your column names will be called over there so guys you can skip the few columns like ID that is primary key and you're created and updated at so basically uh by default the ID and created and updated are imported by default so you don't have to import them but rest of them you can import it okay so guys let me just browse again and copy this names here and we'll be removing what all columns we don't require so now let's type here protected dollar fillable f i l l a b l e f level equal to inside array you are going to paste all your column names so let me paste here and you need to give them inside single Cotes then comma again single Cotes let me and we do not require this created and updated you can remove them and here is active done so Guys these are my fillables so why we are using this you are uh inserting the data okay using this model so we have a create method or create many methods which requires this fillable option okay so now we have understood why this fillable property are we using here okay so it to import your column names so now guys let us see with another property about casting so what is this larel casting doing here so let me just type that uh property name here protected dollar costs equal Al to in Array okay so dollar cast what is this doing so you can cast any of this of column names okay so right now here I see is active is like Boolean type so I can mention that is active it's going to be a Boolean so whenever you insert any record it checks for the boo value and inserts that buum value okay so for example here I don't have any other form but let's understand you have a start date and end date so let me just type here startor date which is going to be the format of date okay either you can add a date and time also so let me just add here date time and you can specify that end date here okay and if you want to give the format also you can just follow with this of y hyphen m hyen d so this is your month and date you can follow in this format while inserting the date time or just date so this is all about casting so if you want to understand more what or things are provided by the larell you can just go through their larell documentation and you can follow about the casting so let's get back and now guys let us see with another property that is append so let me just show you with the example here let's type protected dollar append equal to in Array format so now guys what does this appens do so appens is nothing but like whenever you fetch your model like you are going to fetch the data using this model at that time you want to append one new column as like product name and price so while append this just add name underscore price so I want to keep in this format and here what data you have to keep that is your name and the price so I want to concatenate that and get the full product name and price okay so for that how can you append the data inside this column field so you can achieve that using your accessors in LL so let me create one function for accessor so public function the function name so here how the accessory defined using get then your column name whatever you have given here okay so here is my column name so we are using a snake case so let us use a camel case so n m name Price p r i c e price so which understand as a camel case and this is snake case and then make sure you are giving the correct spelling named as attribute at r i b u t e so this is a prefix guys like it is mandatory and this is the get mandatory here and then open and close Cali and now you can tell like dollar this of name so here dollar this of name okay so yes let's return dollar this of name and you can concatenate inside single codes use hyphen space and dollar this off price so this will can continute your product name and price and append a new value inside this and while fetching your data of products so it will auto automatically add this column name here okay so guys let us uh see with an example so let me just insert one new record quickly here insert and t-shirt copy and paste again then the original price is like 500 450 I have around 50 and created and updated at you can just select the current time and date yep and now guys let's save it and here is activ column is there which is going to enter by default one so let's click here go okay let's browse and here you see guys we have inserted the record and now let us try to fetch that so let's get back and yep let me go to the routes web.php so let me remove all this we not require here and use a get method and just use a products route comma function function open and close CRA and here you can type your model so return product colon colon get method okay so which means it will get all the data so now you have to import this product model import you have imported the model and now let us copy this products and see the output here refresh perfect so guys you see that we have appended the data so let me just go back to your model and let us comment this code first okay your append property code is commented now refresh let me zoom in so guys you see that we have all the details here and if you want to hide this uh created that updated ad and this ID you can just use this simple property about hidden so protected dollar hidden equal to I made a spelling mistake hidden and mention that created underscore at and now let's see the output refresh perfect so you see that that created at column name has gone so same way you can add the updated ad also okay either in single codes or double codes let's save so you see that property hidden also we have learned refresh okay and now guys uh what we were discussing about append so let me open this un comment name price so let's refresh here so here you see under this column name undor price we have a T-shirt and then the price so which is appended over here okay so now guys let us learn with the new attribute of your elquin model that is primary key okay so it's not mandatory here to give primary key because L will understand uh we have already set the primary key here let me zoom out so here on this click on the structure products table so ID is by default primary key so you don't have to specify that my ID is called primary key by chance if you're creating a productor ID and then you want to specify the them so you can utilize at that time so let me just show you how you can do that so protected dollar primary key equal to single codes and semicolon so here you have to mention your primary key ID so this column name is my primary key okay so it's not an issue guys if you want you can keep or else you can just comment it out so I don't prefer using this because if you're using ID so please don't declare if you're using a different ID like product ID so at that time you can utilize this and now let's begin with the next topic that is about accessor and mutator so it is nothing but get and set attributes so as you have seen that in accessor you see with one example here and now let me just type that ACC e SS ORS accessor and mutators in l okay so first we'll see how accessor is used here so it's the same format let me type the function here public function and the function name so first you have to use this get and attribute function so these are the keywords and between this you're going to specify the column name so now I'm going to tell that this is my column name okay so which is matching to my fillables okay so here uh let us make this Capital so you can use a u UC first function so which is a PHP of function to make it capitalize okay and dollar name so now get back refresh okay so we get undefined property that do dollar name we are not able to find so why it is doing like this because it is not able to identify what is this dollar this of name so we know that it is a fillable column that is your name okay database table column name but it is not able to identify so for that you can just tell like attribute of array and then paste your column name okay have just misspelled here attributes it should be now see here cool so now guys you see that uh our t-shirt values it is same so let me just make it all small letter here like T hyen Sr t-shirt so it's all lowercase now and refresh so here you see it's uppercase now okay so first letter is coming in capital but you have entered the value here in the name as small small letters so now guys you understood how your accessor works for this and you can use another way accessor which is I have shown here okay so you can utilize in many ways to use this accessor property okay so guys we have seen with the accessor and now let us see with the mutator so how does mutator work so guys we are going to take the same example with the attribute name okay so same column name so guys let's create a function public and use a set so I'm going to take the column name name here and then attribute function okay miss the function here guys and now guys you can set your attribute data so guys how this set attribute works so make sure uh this column name is available your in your fillable so here you see in the fillable we have that name okay so we are using that here here and whenever you try to save the data using this model that time it tells like go in the column name and give that name here I mean value whenever you save the data that column name it tries to fetch and tries to set the new things over here so dollar this of attributes array of column name equal to product name as capital okay so let us tell St str2 upper function and paste dollar value okay so whenever you save the data it comes in uppercase and push the data over here okay so this is about the mutator how to set the attribute so now guys uh let me just show you with an example so let's go to our web.php and let me copy this and paste here and guys you can just hyphen create and use a create create method just open and close array and fill all the column field names so you can get back and let us copy this paste Here and Now guys let's fill the product name so let me as a all in lower case man pant style okay so copy paste and tell your description again paste for the small description something like that I have 40 stock and active it should be one and so now guys let's copy this URL and try to create one new record so while creating the new record you have a name attribute right so it should store in a capital wise so as we have set the mutator here okay so let us see how we can work here let me open a new tab hit enter okay so here you see guys that our new record has been inserted into our database let's move browse perfect so guys you see that we have a a new record but our name is moved to Capital we inserted a small letter value let me just show you but it has changed to uppercase okay because of this set attribute that is your m mutator in LEL so guys same way you can utilize for your product slug also just create your migration for the slug and let me just give you example okay so for example you have imagine that you have added a slug and you want this name value to be converted as a slug so we can just add a Str Str colon colon slug function and then paste your value inside that so it just changed that into a slug okay so let me import this s Str so okay so we have imported let me just show you it's imported and now guys make sure you are adding in your fillable so it understands what it is okay so here you add it done and now you can get back so if you see that we don't have a slug column if you want you can just create that manually or use your migration command so guys let me just try with and show you so this will be a last example so PHP artisan make colon migration and add slug to my products table unor table and if you want you can use a flag that is table equal to products hit enter so it creates a migration file you can find that here in the database migration and slug okay and now guys let's use a dollar table your string that is whereare and the column name I want to keep as Slug and the column length 500 and I want to add this column after the name column okay and add the column name name and let us make it nullable and now make sure the column name is correct so here you see we have a column name so yes it is correct and now also in the down function you need to add that whenever you roll back it should drop the column so drop column that is Slug slug okay so we have created the migration file let's migrate this PHP Artisan migrate hit enter so it's migrated guys okay so let me just show you browse here so you see that a new column is added that is Slug after the name not at the last as you see is active Okay and now get back so we have created the migration and now whenever we try to insert a new record it will automatically insert a slug value also so let us just test with the same data so let me add manstyle two okay man Pan 2 and let's refresh here so we see okay here you see our data is inserted and you see that slug also has come so let me just browse and show you cool so here you see that man Pan 2 which is converted to slug your product name is into slug perfect guys so guys we have seen that how we can utilize the mutators or set attributes in larl okay so guys if you want more detail about attributes you can just go through the larl documentation about accessor and mutator in larel so guys in this video that's it thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 1,948
Rating: undefined out of 5
Keywords: fundaofwebit, eloquent model tutorial in laravel, what is model in laravel, eloquent model in laravel, laravel model step by step tutorial, laravel eloquent model tutorial complete guuide, complete guide on laravel eloquent model step by step, laravel 10 tutorial model, laravel model step by step, eloquent model tutorial with example, how to create model in laravel, what is orm, laravel 10 tutorial, accessor and mutator in laravel model, getters setters in laravel model
Id: 99osfa6IbC8
Channel Id: undefined
Length: 21min 0sec (1260 seconds)
Published: Thu Dec 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.