Codeigniter 4 CRUD Operation | CRUD Application in Codeigniter 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to start with the crud application in codeigniter four point plus versions okay so guys now for uh crud application we will get started with one employee detail okay so let's create one table for that so make sure you have created so let's create first table where i'm going to create in codeigniter for database called employee table let's create the table name called employee yes and id will be default i mean id where integer 11 and then we have to make it to primary so let's make it primary over here and go and let's increment the auto increment done and now guys let's insert and give first name fi rst underscore name first name where where care will be and value will give 191 and then second column will be last underscore name where this also will give varchar 191 and then over here you will give phone number phone number we will take this where care and we will give this as 50 or 191 default you can mention so let's follow up with 50. more two columns which are required for uh email and designation and one more thing which will be created so let's increment with three columns let's add that three columns over here done let's add it email designation and created underscore at okay so now let's change this to where cap and this will be as 191 we will check it let's make this also work our designation and not worker will make it to medium text medium text not medium int okay so let's give no values value will be assigned automatically and this one we will make it to timestamp so this timestamp will make it to current timestamp it should automatically enter the timestamp so let's save this let's click so processing request and a table is created now as you can see that employs table is ready so let's get started with that so let's get back to our vs code and this is our fresh application now over here as you can see this is the page so guys now when i click on this the employee url we have need to create and create the page so let's get back and let's go to our app folder views we want to create one folder called employee so let me create employee folder inside that employee i am going to create one new file called index.php file as simple and inside this we need to design the layouts so let's get started with that so before designing layouts i have already started with that where in front end i have added those okay so added means like you have to link your bootstrap and make all this layer sections so guys now let's we have already rendered this section so let's include this section into our file and extend this file to in this page so let's call that okay so let's paste here guys so inside my layouts folder i have frontend.php so that file only i'm calling and then extending the section so section is over here as you can see that section of render section which i have called it so let's close this file and make sure you have the set the bootstrap so it's now to view this page we have to set the route first so before setting it out let me just give one container container where row then column md12 where inside this card header header and then card body simple so before this card header you need to give card okay and this card header we have to make it card hyphen header okay and inside this h4 tag which will be add i mean employee data as simple let's save this and let's view this page so before viewing we need to go to config and check with routes.php so let's click on routes.php over here and let's add the route for it so let me just paste with the route start with route called employee data crud for employee data where dollar ruts routes of get function inside this get function i am going to call my route called employee copy and paste and then give forward slash and let's create a controller for it so what controller you are going to create employee controller which will be at index function we need to create that so let's create a controller first let's copy and go to your controllers folder inside that right click and create new file employee controller dot php where you need to write the class for it so first open php and include the namespace of it so let's call namespace with your inside the app folder so app folder and then controllers folder because you are inside the controllers let's see a semicolon and then simple create a class for it let's create a class called employee controller which extends your base controller okay base controller oops base controller so this base controller above base controller we want to extend that simple and now guys inside this only we will be doing our code so over here you need to going to create a page i mean function called public function index index function and inside this you are going to return view where the path so what path you are going to call this index page so inside views folder employee folder and index.php as simple so let's type employee folder forward slash index.php if you want to extend extend or else just do index only so let's save and check the output of it let's refresh you can see employee data could so let's get back to our employee page design over here let me just add margin top five oops so let's refresh you can see that data is here so now let's insert the data first so before fetching you have to insert the records so let's insert a dummy details from here only so let me insert some data you can see let's id is auto incremented so ignore that let's insert call full name where i'm going to insert first name with and then prakash n and then phone number so triple a triple a triple eight and let's give way at gmail.com and then designation as web developer let's create a stamp is there so let us fetch one more detail called om prakash o m om prakash n and then phone number double nine double w999 home at gmail.com designation is django python developer okay let's save and we need two records so we'll ma for this testing so let's move with that let me just browse over here okay so you can see two records we have inserted by directly into database now let's fetch that data over here so let's get back and yep guys so let's move to our controller okay so from here you are going to call the model so how will you call so before calling your database table you need to create a model so let's go inside the models folder over here and create one model called employee let's style employee employee dot php so this will be my employee model done and this will extend that so let's call this open php and close php as simple and call the namespace okay so this namespace it's inside app folder inside app folder your insert the models folder let's call model mode ls models folder semicolon and then you need to create a class class called employee so this will be my employee model employee where extends the model okay and then open close so this model will be used from your codeigniter so let's use that you can see that it's used top directly so when we hit enter it automatically imports okay so you need to extend just install your extension for that and now guys let's give a protected and give table name first so my table name is nothing but employee so let's copy this table name and paste and now you have to get the primary key so let's make this also protected dollar primary all inspects small letter and k in capital then again small where we are going to tell this primary key is id and now the allowed fields we need to check with that protected allowed fields and inside this you need to pass all the fields which you required to insert so those are nothing but this let me just browse with the structure let's copy and paste so first name need to copy and paste over here so same way you need to input all the things so let's copy that last underscore name then email phone and then final designation so let's copy that also designation and paste over here that's it let's save this and fetch this data so let's call this over here so let me just type on folder called i mean variable name employee and inside this we will call our model so my model is employee done either you can give function also not required if you can just ignore with that so let's use this over here use inside app folder forward slash models folder forward slash your employee data okay so this is how you import that let's use that over here and let's pass this variable into the view page so let's create one variable called data and inside this data of array where i'm going to pass this so use find function find all capital a and then give semicolon so this will fetch all the records from your employee table and let's pass this employee name so with this help of name we are going to fetch that so let's pass this variable data inside this index page so let's pass and let's copy this and fetch into this index page let's go over there and fetch the data over here so before fetching need a simple table design so let's add that table border where it's very simple let me just finish with that as soon table row table heading where first is id first name then last name email phone designation and then created at if you required you can add all or else remove that so let's add one final action which will be there simple same way you need to fetch that over here in table body let's give table row and inside this td you are going to fetch over here let's type the code for it so open php and close php let's give for each function and let's give colon over here okay let's pass the variable called employee so let me just copy that employee dollar employee as dollar these are row items so let us give row and your for it is started so let's end the forage first php open and php close and let's end for each and give semicolon oops just remove that parenthesis and now over here you are going to fetch record so let's copy this dollar row and echo that so this is a shortcut method to echo where i'm going to echo the id save and now let's refresh and see let's refresh over here you can see one and two rd is coming so let's switch all the details of it over here right now we have inserted two data right so let's see now copy paste base paste and then just type in the first underscore name all your column names of your database so last name and then email phone design nation same and created underscore at and then one final thing which will be td where you can access those for edit and delete okay so let us just create one simple button for that edit class where simple button button button for edit we can give as success where button small i will design with that copy and paste same thing and make it to delete and from success to danger simple save it and let's check the output okay so very missing is there undefined index why spelling mistake what is that designation spelling is missed over here so let's record this designation spelling is wrong so let me just change this okay let's change this variable name i mean column name designation d e s i g n a t i o n so let's save and let's go and paste that same name that's it save this and let's get back and refresh once again so created at also one is missing here it's correct spelling let us check over here okay let's paste that let's refresh so next cool so finally we have fetch the data now so guys let's make one insert button to insert the data from here only okay so now guys we will be inserting the data so let's get started with this as simple we are on the employee and now let's move to our vs code editor and then over here this is our heading so let me add one button to insert so let's create one button called with the a tag as add employee and over here we'll give a class button button primary where button float right okay and let's create one echo where you will create base underscore url for add or you can tell employee hyphen add okay so let's copy this and create that into our route so before creating route let me just refresh and show you the data so you can see over here okay it's bootstrap 5 i think so let's make it to end let's refresh you can see that okay so let's click on this you can find a url called employee ad so let's create a route for it go to routes and now let's create one route the get method dollar rots routes where get function where inside this i am going to pass this path and then go to your controller so in previous audio only we created the controller so let's copy and paste over here and tell this create page okay so let's create a function with the name create in your employee controller so let's move over there and create a function public function where method name will be create and inside this you are going to view the page so return view oops let's tell over here employee forward slash create dot php file so let's copy this and go to employee folder this is our views folder inside views we have created employee and inside this will create a file called create.php hit enter and your page is created now so let's design this file and start inserting the data so before that i'll just show you some output of it okay so this same design we are going to use just instead of table the form will be there so let's move on and copy that form so this is my index page let me just move over here index page let's minimize this table and copy all the things required into it paste let's copy and paste let me just minimize this sidebar let's make it to back danger and this will be employee only over here employee add employee done let's view this page refresh you can see the output cool so let's click this back you can see employee let's add employee done so let me just remove this question mark and let's create a simple form so how what forms you are going to create the table required designs so we have this many columns in our database so these columns only we are going to create like first name last name phone email and designation so this five fields we are going to create with that so let's get back and start creating with that okay so row where i'm going to start with the column md 6 over here and now let me form group f o r m hyphen g r o p group and here label will be there and let's give this label for called first name input dot form control this will be for your first name so let's give name attribute where it will be first underscore name and placeholder please holder is equal to first name enter first name save it and let's see the output refresh okay you can find that right so let me just design all the required fields till that so guys now i have design let me just paste that done as simple you can see the same name which i have created for first name then last name with the name attribute called last name email phone and designation with one simple button that is called submit okay and then let me just refresh and show you the output so let's refresh our form is ready now so let's start the code for inserting the data into our database okay so let's begin with that done and now uh this is our form where this row starts over here so from this row before and ending at that place we are going to create a form and this form action over here and let's cut this and paste below to it and over here you have to include like method will be post p post post and over here your action will be is equal to where open code base underscore url where you are going to send the data so what data you're going to send is employee save okay so let's copy this and go back to our route so instead of steve let's give store okay so this will be better nice to remember so let's copy this and move to your routes and let's create a route for it so dollar routs routes where with the post method we are going to send okay so inside this post method first we have to declare the route and then call your controller so let's call the controller inside single quotes we are going to call the controller at store function okay so let's create a store method into your employee controller let's go back over here public function where storefront method and over here you are going to type all the data so first where we are going to store into our employee model so make sure you have created so let's call this model over here let's call create a variable called employee and where new you will tell employee control i mean model okay so this employee model name same name this name we have to call over here and use that at top once done let's create variable called data and inside that you are going to store the data whatever you are getting from the input so let me just give first underscore name so why this is we are taking this is for to store in data so this first name last name that we are calling over here so what data you are going to pass over here is nothing but dollar this request of your getting the help of post method okay we use the post method so what data you're getting that so let me just tell you where in create.php you are getting this first name let's copy that and send over here as simple let me just remove this semicolon because with that it will show error let's paste for everything and let's change this to last name okay and then give the same because we have already given the same names for this you can see that so email phone and designation so let's copy this and create for this three which will be email and phone and one designation so let me just replace with that over here designation and then the same for this also email column and phone column done so these are the few details we want to insert into our database so now guys let's use the save function so let's call this employee variable over here and call the save function and inside this save function you are going to pass this dollar data so let's copy this dollar data and send over here as simple and after saving let's return and redirect so let's tell oops let me just redirect function to where i want to redirect redirect let's redirect to and after redirecting where with this function i want to send a message okay so first let's type that base underscore url i want to redirect back to where so employee page over here so let's go back where this page so this page should be shown so let's copy this url and paste over here and with means we are going to set one status with the help of with function so with function on works on sessions okay so where you will tell employee added employee add ed added successfully okay save this and now guys let us store and see so before storing this let's create this status to check the message so let's go to employee url so which will land on index page so let's one to view the message so guys i have already coded so let me just paste with that as simple there is nothing much in this where you can see the session is set when it is set the time it will get if it is set means we are going to echo in h4 tag where i am showing the status so this status will work on with this with status so let me save and refresh the page okay cool and now let's add the data so let us test with that so right now you can see only two datas are there so let me add one more data called gmail.com and phone number some digit and over here we will tell business bispns business me click on save so you can see that you have got the data and you have fetch the record okay designation did not insert over here so let's just refresh in your browser also and see that relate is not inserted why it's not inserted so let's check that okay so input tag we have to check first this is correct spelling okay our column name so let's check this input tag it's given perfect or not so let's paste that over here it's the same okay so it is not getting allowed y so let me just check in the model table over here okay so here is the spelling mistake so guys make sure you are not giving any spelling mistake if you give spelling mistake it will not save the data so which means allowed fields only will save it here so make sure you are giving correct spelling and let's get back and let's add one more data to see that it's working fine so we can tell mahesh kumar where mahesh gmail.com where phone number and tell bca employee let's click on save so let's saved and you can see that data is added perfectly so let's browse over here okay cool so this is how we are saving all the data so let me just add over here simply that's business man because we missed that right so for that sec didn't save so guys we have successfully inserted the data and saw that message also so guys as we saw in previous that is about fetching the data and adding the data of employee okay and now in this we will be covering about how to edit and update the data so guys let's get started with it so first move to our editor and over here you can see that it's our index page so index placement this page okay and the table of this is over here let me just open this table and you can see that data and this is our edit button right and now let's code with this so first step we will open php is equal to then close php directly to echo that so let's call base underscore url function inside this you are going to call the path so my path will be employee forward slash edit forward slash and the employees id okay so let's give dot and concatenate this data dollar row of id so let's copy this and paste over here and let's give semicolon that's it not semicolon in function we don't require so let's save this and now same we are going to create about this so first let us see it is working fine or not let's get back refresh and let's click on this second row okay over here i'm going to click on edit you can see that employee edit and then it's id so let's copy and create a route for it so let's go to your routes.php and now over here let me just create the route r o u t s routes where we are going to use get function oops let's give semicolon and then inside single quotes you are going to paste this so for any passing the parameters in your url that time you are going to use this parenthesis where you are going to call this any any you can call or call the number which will be okay so we are passing the number so let us call the numbers and let's call the url for it i mean controller so let's call the controller over here so we have already created the controller and now let's tell this function name as edit so let's create when you're sending any parameters or any numbers so we need to send in this function also edit function so give forward slash dollar one okay so dollar one it is so once when you give this this will be assigned with that variable thing so let's copy this and go to our employee controller and create a function let's move and over here let's create the function public function where edit and we have oops let me just open with curly braces and now inside this function we have to take the id okay let's give in small letters and id so this is past how with the help of this route so over here you are sending and how this is coming from this okay from your url so let's get back and create the page for it so let's return view where you're going to return the page let's create employee folder inside employee folder i'm going to edit dot php file okay if you can include means import this or else just ignore this okay so let's create a edit dot php file let's go to our views folder inside employee folder i am going to create one new file with the name edit dot php hit enter and edit.php file is created so guys we need some design to do that so before designing let me just show you a demo let's say hello and refresh the page you can find that page edit one so guys now for this editing the data we can use the same form which we have used in add employee okay so this same form we are going to use for the editing the data so let's get back and copy the create page let's go to create.php give control a and copy and paste the same form as simple and remove few things so first let us check the output and according to it we will remove it let's get back and let's click on edit with the id2 okay and this is our form over here let's change this title and the details of it so first we will make it to edit employee and the back button will be the same and now over here let's remove this base url for right now when we click on submit that time we'll check with that so let's use enter and over here you need to fetch all the data so for that as simple you can see and let's make it to update okay and let's get back over here guys so now we need to fetch all the data so let's go to our employee controller at edit function let's follow that so let's call that create a employee variable where you are going to call your employee model okay employee model and in this employee model you are going to find the data which data you are going to find find with the help of this id id number two so how will you find that so before in doing that you need to import this or else use in this class so make sure you have added your model and this is my model over here as simple okay now let's get back to our controller and let's go to our edit function and over here let's oops let me copy this and paste where i'm going to find use find function given by codeigniter and let's call that id and this id will be called and let's send this so let's create a data variable variable data of array okay is equal to and inside this you are going to call the variable called employee okay and now copy this and send to this page as simple data done let's copy this and go to edit page let's move and over here you are going to paste in that so let's give value and uv is equal to and insert this php open and close and inside this you are going to echo so how will your dollar employee of what your first name of your column so in my table you can see that codeigniter 4 inside this you need a employees table so you can see that first name last name email phone and designation so these fields we are going to require that so let's copy and call the same things okay let's copy paste last name and then email let's copy and paste email has done and now phone copy and paste for the designation also you can just copy and paste where the same name of it so let's save this and now let's check the output of it where we'll refresh this and now you can see all the data it's coming fine so let's get back and check with other id also let me click to id number four okay so let's edit on this you can see that my data where i default it's coming cool so guys now let's start with the updating of this data so let's go back and let's first you need to update the form url so let's give employee update give forward slash okay update with the id so let's call this id over here let's copy and let's paste let me concatenate and then paste it so over here you are going to call id not the first name so let's take this and update with that so go and create a route for it let's move to routes.php we are going to tell routes where i am going to use the post method to update the data okay paste and forward slash we are taking id so let's pass in numbers and then create a controller with that so let's take the same thing we because we are passing the parameters over here number integer that will be sent to this function so let's create a function called update in the same employee controller so let's copy this name and create a function in it public function where update let's close and open okay and now you have to write the code first so let me just copy this same thing over here and the same id also okay so you are going to find that so let's pass that dollar id over here and inside this we are going to get this data same thing so let me just paste with that same you are going to find and then after finding you are going to update the data so what are the data you are going to update from the input method so we have done already in our to store that so let me just copy that same thing and paste over here done and now with this help what is it this is nothing but your database column names so these column names first name last name email phone and designation so those are the column fields over here and these are the input tags so this first name last name email phone and designation are nothing but your input tags name attributes so these are the fields over here okay phone and designation and then common submit button to update that with the help of this route okay with this post method so let's get back to a controller and over here make sure you are passing the correct names done so once it is done let's copy this again variable same employee variable where you'll copy and paste and call the update function okay so in update function you need to pass that dollar id first and then you will pass the dollar data dollar data sent over here okay so you are doing directly over here so we don't require this method also so okay so let's remove that and directly we are passing that id on this id you need to update this data we are telling so once updated you need to return and redirect to some page right so let us just copy the same thing and paste with that to return and redirect web base url where i'm going to employ with the status employee updated successfully so to view the status we have already done on this employee page where i'll just show you that code okay so this is the simple code to view the message of the status let's get back to our controller and let's save the code so guys now let's try to update the data get back refresh and now let's change this designation to some company employee okay let me click on update i have clicked now you can see that company employee it's updated and you can get the message also employee updated successfully so guys we have successfully updated so let me just change this from this id number one web developer to full stack developer so let's go back full stack web developer let me update update employee you can see that it's updated successfully so guys if you want one more method to update like from the post method to put method okay so let me just show you if you're required to use put method so what is that put method is nothing but from your route over here you don't want to use post method you want to use put method when you're accessing this port you need to update over here in your edit form so below this form only let me just show you to update that let's give put and over here make it to hidden this and let's give a name attribute oops not namespace name attribute with underscore me th od method and value which will be put put p u t put method so that this is how you are going to update with this input tag okay so let's save this and let's update the data using put method so these are the very basic changes input method so let's get back refresh the data page and now let us edit so i'm going to edit this id number two data let's click on it and let's remove this django to direct python developer so i'm going to click out click update okay we clicked on it and it's successfully updated you can see that python developer hey guys welcome back so guys as we are continuing our crud application for employee data in codeigniter 4 so guys till now we have completed our adding the employee fetching the employee editing and updating the employee and now in this we will be covering about deleting the employee so when we click on this delete the time this record should get deleted okay so let's let's get started to implement that so let's move to our table and here you can see our delete button which is shown over on this google chrome and guys let us update with that so let's create a url for it so open php and close php where inside this we are going to give up url which will be base underscore url function and inside this we will call the path of it so we are going to set a route let's give forward slash delete with the id so id you need to access we'll get dot concatenate and then we will call the route id of it employee id and now with this help of path we will delete the record so guys let's create a route for it so let's go over here and let's create a routes routes where i'm going to use get function we'll just call this path and delete the data where forward slash into a parenthesis where number we are going to send and then at the same controller we are going to pass that with the id okay let's pass with the id and we'll go to delete function delete method so let's copy this and go to employee controller and create a delete method over there so let's create one function public function at delete and inside this we are to delete the employee data so employee data means like our model so let us create one variable called employee is equal to where we are going to call our employee model so this is my employee model done so when you are calling this you have to call at the top you have to make sure you are using this so this is my model over here so which i have created for my table okay so let's get back to our controller with the delete method and over here let's copy this same variable and paste over here and call that delete function delete delete function okay and inside this you're going to pass the id dollar id so what dollar id are sending over here so this will carry the numbers right as per your route so you are sending here the number digits i mean employee id so that employee id will be passed with this indeed delays method so let's go over there and you can see that and take that id variable and delete the data so once if you send over here the id of this table will be deleted and once after deleting let's get back and paste the same code let's tell over here return and redirect to same page called employees so employ means this page only will be redirected and then again you can tell this as employee deleted successfully let's save and refresh here guys so let me just add one dummy detail and delete that let's add dummy dummy where dummy gmail.com some phone number and some designation save the record is saved so let's click on this to delete so let me just refresh this message will go and let's click on delete so i'm going to click on delete button where you can see that employee deleted successfully cool so our data is deleted and come back to same page so guys this was the one method which we have used to delete the record okay so guys now let us use one delete method to delete the record so let's i mean like when you create a route right we have used the get so instead of get i want to use delete so let me just copy and paste and let me comment with this and where i want to use delete method okay so this delete function should be there to delete this record so for in this case you need to use uh let's go to index and create a for that let me just minimize this sidebar and let's create a form let's comment this create a form and inside this where you're going to pass action is equal to and method is equal to you can see post post and one input tag that will be hidden and this name is equal to where you are going to send underscore method value which will be delete d e l e t e okay delete simple and now same url you're going to copy same base url let's copy and paste into your form action done and then one submit button so let's create a button called delete and the same class you are going to copy and paste that inside this delete button so before that class you need to assign that submit let's give submit and save the data so guys it is commented where you can find let's save this and now let us test with that okay and check so let's get back refresh cool now let us click on this delete so let me just click on delete on this fourth id number four click delete where you can see that employee deleted and id number is for not that okay so you can see in the database also it will not be there so this is how we use the delete method to delete the record so this was the second method just for knowledge purpose i have thought on this so that's it in this video guys and thank you for watching this video please subscribe like and share
Info
Channel: Funda Coder
Views: 14,204
Rating: undefined out of 5
Keywords: fundaofwebit, funda coder, crud in codeigniter 4, codeigniter 4 crud operation, crud application in codeigniter 4, codeigniter 4 crud, crud in ci4, CRUD in ci4, CRUD application in codeigniter, how to insert data into database in codeigniter 4, how to save data in database in ci4., CI4 crud, CRUD in codeigniter, simple crud in codeigniter 4, simple crud in codeigniter, crud tutorial in codeigniter 4, codeigniter 4 crud tutorials, codeigniter 4 crud crash cbourse, tutorial crud
Id: g44PAu1c5G4
Channel Id: undefined
Length: 52min 26sec (3146 seconds)
Published: Sat Jan 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.