{"id":5570,"date":"2021-04-22T03:14:08","date_gmt":"2021-04-22T03:14:08","guid":{"rendered":"https:\/\/programmingfields.com\/?p=5570"},"modified":"2021-04-22T03:26:16","modified_gmt":"2021-04-22T03:26:16","slug":"create-a-crud-application-in-codeigniter-4-for-beginners","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/","title":{"rendered":"Create a CRUD Application in CodeIgniter 4 For Beginners"},"content":{"rendered":"\n<p>The CodeIgniter 4 is launched with the updated features over the CodeIgniter 3. Now, it is a little bit similar to the Laravel framework. You can create the database schema using the migrations in CodeIgniter 4. It will work like version control of the database schema. Every time, when you will update the schema, you will have to migrate it. The migration will drop out all the existing tables and schema and then recreate it. So, it is pretty easy to use and manage in CodeIgniter 4 application. Also, you can create a <strong>Model<\/strong>, <strong>Migration<\/strong>, and <strong>Controller<\/strong> file using the command line. These features were not available in the previous version. Today, in this post, I will create a <strong>CRUD application in CodeIgniter 4<\/strong>. The CRUD operation in CodeIgniter 4 will contain the CREATE, READ, UPDATE and DELETE on the basis of data.<\/p>\n\n\n\n<p>In this CodeIgniter 4 series, I have started from very scratch. So, that it will be helpful for beginners to grab and implement it. Hence, I am starting from the <strong>CRUD in CodeIgniter 4<\/strong>. So, let&#8217;s continue to the post.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_a_New_App_For_CRUD_in_CodeIgniter_4\" >Create a New App For CRUD in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Database_For_CodeIgniter_4\" >Create Database For CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Configure_Database_in_CodeIgniter_4\" >Configure Database in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Migration_in_CodeIgniter_4\" >Create Migration in CodeIgniter 4<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Migration_for_Post_Table\" >Create Migration for Post Table<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Model_in_CodeIgniter_4\" >Create Model in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Controller_For_CRUD_in_CodeIgniter_4\" >Create Controller For CRUD in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_CRUD_Operation_in_CodeIgniter_4\" >Create CRUD Operation in CodeIgniter 4<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Explanation_of_PostController\" >Explanation of PostController<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Namespace\" >Namespace<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Constructor\" >Constructor<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#CRUD_Methods\" >CRUD Methods<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Routes_For_CRUD_in_CodeIgniter_4\" >Create Routes For CRUD in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Views_For_Crud_Application_in_CodeIgniter_4\" >Create Views For Crud Application in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Render_Data_on_View_in_CodeIgniter_4\" >Render Data on View in CodeIgniter 4<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Master_View\" >Master View<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Create_Post_View\" >Create Post View<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Post_Edit_View\" >Post Edit View<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Index_view_to_display_all_posts\" >Index view to display all posts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Show_Post_View\" >Show Post View<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#CRUD_Operation_Results\" >CRUD Operation Results<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Posts_Listing\" >Posts Listing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Display_Single_Post_Result\" >Display Single Post Result<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#EditUpdate_Post_Result\" >Edit\/Update Post Result<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Delete_Post_Result\" >Delete Post Result<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span><strong>Prerequisites<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating this project in CodeIgniter 4, your system must have the following configurations.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>PHP &gt;= 7.3<\/strong><\/li><li><strong>MySQL (version &gt; 5)<\/strong><\/li><li><strong>Apache\/Nginx Server<\/strong><\/li><li><strong>VS Code Editor<\/strong><\/li><li><strong>Composer<\/strong><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_New_App_For_CRUD_in_CodeIgniter_4\"><\/span><strong>Create a New App For CRUD in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating the CodeIgniter 4 application, I will be using the composer. If you are new to CodeIgniter 4 then I must tell you one thing. Now you can create the CodeIgniter 4 application using the command line with the help of composer.<\/p>\n\n\n\n<pre title=\"create codeigniter 4 app using composer\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">composer create-project codeigniter4\/appstarter blog<\/code><\/pre>\n\n\n\n<p>In the above command, the composer will start creating the project. The&nbsp;<a href=\"https:\/\/github.com\/codeigniter4\/appstarter\">CodeIgniter 4 app starter<\/a>&nbsp;repository holds a skeleton application, with a composer dependency. Here the project name is <strong>blog<\/strong>. So, it will create a new folder with the name blog, and install the files of CodeIgniter 4 inside it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"810\" height=\"542\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-20-37.png?resize=810%2C542&#038;ssl=1\" alt=\"Installing CodeIgniter 4 Using Composer\" class=\"wp-image-5574\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-20-37.png?w=810&amp;ssl=1 810w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-20-37.png?resize=300%2C201&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-20-37.png?resize=768%2C514&amp;ssl=1 768w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><figcaption><strong>Installing CodeIgniter 4 Using Composer<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>After creating the project, we will create a database in MySQL. For performing the <strong>CRUD operation in CodeIgniter 4<\/strong> we will have to connect the application with the database.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-install-codeigniter-4-in-windows-and-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Install CodeIgniter 4 in Windows and Linux<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Database_For_CodeIgniter_4\"><\/span><strong>Create Database For CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For the database, I am using the command line. You can use <strong>phpMyAdmin<\/strong> or <strong>MySQL Workbench <\/strong>too. It is totally up to you that how you will manage it. <\/p>\n\n\n\n<pre title=\"create database\" class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE DATABASE ci4_blog;<\/code><\/pre>\n\n\n\n<p>I have created the database using the above command. Now, you can see, currently, there is no any table in my database. We will create using the migration in the upcoming steps.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"167\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-27-42.png?resize=840%2C167&#038;ssl=1\" alt=\"MySQL Database For CodeIgniter 4 CRUD\" class=\"wp-image-5584\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-27-42.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-27-42.png?resize=300%2C60&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-27-42.png?resize=768%2C153&amp;ssl=1 768w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><figcaption><strong>MySQL Database For CodeIgniter 4 CRUD<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Now, open the created project in the editor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configure_Database_in_CodeIgniter_4\"><\/span><strong>Configure Database in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We have already created the database. Now, it required to connect it with the application. So, firstly, I have opened the project in the VS Code editor. Just look for the <strong>env file<\/strong> and rename it to <strong>.env<\/strong>. Then open the renamed <strong>.env <\/strong>file. By default, all the environment configurations are commented on inside the .env file.<\/p>\n\n\n\n<p>So, firstly, you have to uncomment <strong>CI_ENVIRONMENT<\/strong> and change the <strong>production<\/strong> to <strong>development<\/strong>. Actually, we are going to create the CRUD in CodeIgniter 4, so it is in development mode. In the development mode, you will be able to debug and find out the errors in your application. But, if you are in the production mode then you will not be able to show any errors In your application.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"417\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-21-32.png?resize=1024%2C417&#038;ssl=1\" alt=\"Change Production to Development in CodeIgniter 4\" class=\"wp-image-5585\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-21-32.png?resize=1024%2C417&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-21-32.png?resize=300%2C122&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-21-32.png?resize=768%2C313&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-19-20-21-32.png?w=1109&amp;ssl=1 1109w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Change Production to Development in CodeIgniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>After that, we will configure the database credentials. So, in the same file, just look out for the database. Here, under the database <strong>default configuration<\/strong>,<strong> <\/strong>we will configure our database credentials. So, just uncomment these lines and put the details as showing below.<\/p>\n\n\n\n<pre title=\"database credentials in .env file\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">database.default.hostname = localhost\ndatabase.default.database = ci4_blog\ndatabase.default.username = root\ndatabase.default.password = root\ndatabase.default.DBDriver = MySQLi<\/code><\/pre>\n\n\n\n<p>You can see in the below result, I have configured the database details.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"933\" height=\"438\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-45-25.png?resize=933%2C438&#038;ssl=1\" alt=\"Database Configured in CodeIgniter 4\" class=\"wp-image-5586\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-45-25.png?w=933&amp;ssl=1 933w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-45-25.png?resize=300%2C141&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-45-25.png?resize=768%2C361&amp;ssl=1 768w\" sizes=\"auto, (max-width: 933px) 100vw, 933px\" \/><figcaption><strong>Database Configured in CodeIgniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Now, we are good to go for the CRUD operation in CodeIgniter 4 application.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/laravel-8-crud-application-tutorial-from-scratch\/\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel 8 CRUD Application Tutorial From Scratch<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Migration_in_CodeIgniter_4\"><\/span><strong>Create Migration in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As I told you at very first, in CodeIgniter 4 we can create the migration file for the table. This migration file will contain the schema for the table. One interesting thing is CodeIgniter 4 provides <strong>spark<\/strong> for the <strong>CLI<\/strong>. It is similar to <strong>artisan<\/strong> in Laravel framework.<\/p>\n\n\n\n<p>Before creating the migration using the spark, you can checkout the help for more details.<\/p>\n\n\n\n<pre title=\"spark --help\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark --help<\/code><\/pre>\n\n\n\n<p>The above command will show you the list of available commands to use in CodeIgniter 4.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"948\" height=\"655\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-54-38.png?resize=948%2C655&#038;ssl=1\" alt=\"PHP Spark Help - List of Commands\" class=\"wp-image-5588\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-54-38.png?w=948&amp;ssl=1 948w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-54-38.png?resize=300%2C207&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-54-38.png?resize=768%2C531&amp;ssl=1 768w\" sizes=\"auto, (max-width: 948px) 100vw, 948px\" \/><figcaption><strong>PHP Spark Help &#8211; List of Commands<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Here, you will see the <code>make:migration<\/code> under the <strong>Generators<\/strong> section. You can also checkout the help for specific command. Here, if we want to see the details of make:migration then you can use the below command.<\/p>\n\n\n\n<pre title=\"description of make:migration\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark help make:migration<\/code><\/pre>\n\n\n\n<p>It will list out the description, usage and the available flags for this command, <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"916\" height=\"363\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-58-00.png?resize=916%2C363&#038;ssl=1\" alt=\"PHP Spark Help For Migration\" class=\"wp-image-5589\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-58-00.png?w=916&amp;ssl=1 916w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-58-00.png?resize=300%2C119&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-07-58-00.png?resize=768%2C304&amp;ssl=1 768w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><figcaption><strong>PHP Spark Help For Migration<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Migration_for_Post_Table\"><\/span><strong>Create Migration for Post Table <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Now, let&#8217;s create a migration for the post. Actually, we have created the blog application that&#8217;s why will create a post table. I defined the table name in the plural so, here the table name will be <strong>posts<\/strong>.<\/p>\n\n\n\n<pre title=\"make post table migration\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark make:migration posts --table<\/code><\/pre>\n\n\n\n<p>The above command will create a new file inside the <strong>app\/Database\/Migrations<\/strong>. Here, I have<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"98\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-48-16.png?resize=1024%2C98&#038;ssl=1\" alt=\"\" class=\"wp-image-5597\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-48-16.png?resize=1024%2C98&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-48-16.png?resize=300%2C29&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-48-16.png?resize=768%2C74&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-48-16.png?w=1102&amp;ssl=1 1102w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Create Posts Table Migration<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>You can see the below result. Here the post table migration is created. By default there are two functions<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>up() and<\/strong><\/li><li><strong>down()<\/strong><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1006\" height=\"371\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-47-07.png?resize=1006%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-5596\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-47-07.png?w=1006&amp;ssl=1 1006w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-47-07.png?resize=300%2C111&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-47-07.png?resize=768%2C283&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-47-07.png?resize=270%2C100&amp;ssl=1 270w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Post Table Migration Created<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>We have to define the schema for the post table in the<strong> up() <\/strong>function. When we will migrate the table, it will generate the schema using the up command. In the <strong>down() <\/strong>function, we will set the code to drop this table. So, let&#8217;s see one by one.<\/p>\n\n\n\n<p>In the below snippet, I have added the schema for the post table. You can simply paste the below code there.<\/p>\n\n\n\n<pre title=\"timestamp_Post.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Database\\Migrations;\n\nuse CodeIgniter\\Database\\Migration;\n\nclass Posts extends Migration\n{\n    public function up()\n    {\n        $this-&gt;forge-&gt;addField([\n            'id' =&gt; [\n                'type' =&gt; 'INT',\n                'auto_increment' =&gt; true\n            ],\n            'title' =&gt; [\n                'type' =&gt; 'VARCHAR',\n                'constraint' =&gt; '100'\n            ],\n            'description' =&gt; [\n                'type' =&gt; 'TEXT'\n            ],\n            'created_at datetime default current_timestamp',\n            'updated_at datetime default current_timestamp on update current_timestamp'\n        ]);\n        $this-&gt;forge-&gt;addKey('id', true);\n        $this-&gt;forge-&gt;createTable('posts');\n    }\n\n    public function down()\n    {\n        $this-&gt;forge-&gt;dropTable('posts');\n    }\n}<\/code><\/pre>\n\n\n\n<p>After adding the schema in the migration file, it will require to migrate. <\/p>\n\n\n\n<pre title=\"migrate table\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark migrate<\/code><\/pre>\n\n\n\n<p>The above command will migrate the tables. Here, we have only one migration file, so it will create the table for it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1006\" height=\"128\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-45-38.png?resize=1006%2C128&#038;ssl=1\" alt=\"\" class=\"wp-image-5595\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-45-38.png?w=1006&amp;ssl=1 1006w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-45-38.png?resize=300%2C38&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-45-38.png?resize=768%2C98&amp;ssl=1 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Migrate Post Table in CodeIgniter 4<\/strong><\/figcaption><\/figure>\n\n\n\n<p>After the successful migration, you can checkout the database. Here, the post table has been created. You can see the schema of the post table has been generated.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"490\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-50-26.png?resize=1024%2C490&#038;ssl=1\" alt=\"\" class=\"wp-image-5598\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-50-26.png?resize=1024%2C490&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-50-26.png?resize=300%2C144&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-50-26.png?resize=768%2C368&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-50-26.png?w=1193&amp;ssl=1 1193w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Posts Table Schema in Database<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Now, we will move to the next step for creating <strong>CRUD in CodeIgniter 4<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Model_in_CodeIgniter_4\"><\/span><strong>Create Model in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To manage and perform the CRUD operation in CodeIgniter 4, we will use <strong>Model<\/strong>. The model will synchronize with the database.<\/p>\n\n\n\n<pre title=\"Create Post Model\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark make:model Post<\/code><\/pre>\n\n\n\n<p>The above command will create a model named <strong>Post.php.<\/strong> Also, it will generate some default codes inside this. Like <strong>table name<\/strong>, <strong>primary field,<\/strong> <strong>allowed fields<\/strong>, and many more.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"105\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-58-46.png?resize=1024%2C105&#038;ssl=1\" alt=\"Created Post Model in CodeIgniter 4\" class=\"wp-image-5602\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-58-46.png?resize=1024%2C105&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-58-46.png?resize=300%2C31&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-58-46.png?resize=768%2C78&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-08-58-46.png?w=1028&amp;ssl=1 1028w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Created Post Model in CodeIgniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<pre title=\"Post.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Models;\n\nuse CodeIgniter\\Model;\n\nclass Post extends Model\n{\n    protected $DBGroup              = 'default';\n    protected $table                = 'posts';\n    protected $primaryKey           = 'id';\n    protected $useAutoIncrement     = true;\n    protected $insertID             = 0;\n    protected $returnType           = 'array';\n    protected $protectFields        = true;\n    protected $allowedFields        = ['title', 'description'];\n\n    \/\/ Dates\n    protected $useTimestamps        = true;\n    protected $dateFormat           = 'datetime';\n    protected $createdField         = 'created_at';\n    protected $updatedField         = 'updated_at';\n}\n<\/code><\/pre>\n\n\n\n<p>We had already created the table name in plural, so it doesn&#8217;t need to specify the table name. It will we okay for this. I just passed the <strong>allowedFields<\/strong> which will be inserted into the table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Controller_For_CRUD_in_CodeIgniter_4\"><\/span><strong>Create Controller For CRUD in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In CodeIgniter 4, you can create a controller file using the spark command. Now, no need to create controller manually and creating the functions inside it. Everything will be auto generated.<\/p>\n\n\n\n<p>If you want to generate the CRUD methods by default then you can put the <code>--restful<\/code> flag after the controller name. This is a very awesome feature here.<\/p>\n\n\n\n<pre title=\"create restful controller\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark make:controller PostController --restful<\/code><\/pre>\n\n\n\n<p>Here, the controller has been created. You can take a look at the controller file.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"92\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-09-08-46.png?resize=1024%2C92&#038;ssl=1\" alt=\"Created Restful Controller in CodeIgniter 4\" class=\"wp-image-5604\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-09-08-46.png?resize=1024%2C92&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-09-08-46.png?resize=300%2C27&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-09-08-46.png?resize=768%2C69&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-20-09-08-46.png?w=1149&amp;ssl=1 1149w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Created Restful Controller in CodeIgniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>When you will open this file, you will find out some methods inside it. Take a look at the below code.<\/p>\n\n\n\n<pre title=\"PostController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Controllers;\n\nuse CodeIgniter\\RESTful\\ResourceController;\n\nclass PostController extends ResourceController\n{\n    \/**\n     * Return an array of resource objects, themselves in array format\n     *\n     * @return mixed\n     *\/\n    public function index()\n    {\n        \/\/\n    }\n\n    \/**\n     * Return the properties of a resource object\n     *\n     * @return mixed\n     *\/\n    public function show($id = null)\n    {\n        \/\/\n    }\n\n    \/**\n     * Return a new resource object, with default properties\n     *\n     * @return mixed\n     *\/\n    public function new()\n    {\n        \/\/\n    }\n\n    \/**\n     * Create a new resource object, from \"posted\" parameters\n     *\n     * @return mixed\n     *\/\n    public function create()\n    {\n        \/\/\n    }\n\n    \/**\n     * Return the editable properties of a resource object\n     *\n     * @return mixed\n     *\/\n    public function edit($id = null)\n    {\n        \/\/\n    }\n\n    \/**\n     * Add or update a model resource, from \"posted\" properties\n     *\n     * @return mixed\n     *\/\n    public function update($id = null)\n    {\n        \/\/\n    }\n\n    \/**\n     * Delete the designated resource object from the model\n     *\n     * @return mixed\n     *\/\n    public function delete($id = null)\n    {\n        \/\/\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here, all the possible methods are generated by default for CRUD in CodeIgniter 4. Now, we have to put the functionalities for the CRUD operation in CodeIgniter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_CRUD_Operation_in_CodeIgniter_4\"><\/span><strong>Create CRUD Operation in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Inside the <code>PostController.php<\/code> file, simply paste the below code.<\/p>\n\n\n\n<pre title=\"PostController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Controllers;\n\nuse App\\Models\\Post;\nuse CodeIgniter\\RESTful\\ResourceController;\n\nclass PostController extends ResourceController\n{\n    private $post;\n\n    public function __construct()\n    {\n        helper(['form', 'url', 'session']);\n        $this-&gt;session = \\Config\\Services::session();\n        $this-&gt;post = new Post;     \n    }\n\n    \/**\n     * Return an array of resource objects, themselves in array format\n     *\n     * @return mixed\n     *\/\n    public function index()\n    {   \n        $posts = $this-&gt;post-&gt;orderBy('id', 'desc')-&gt;findAll();\n        return view('posts\/index', compact('posts'));\n    }\n\n    \/**\n     * Return the properties of a resource object\n     *\n     * @return mixed\n     *\/\n    public function show($id = null)\n    {\n        $post = $this-&gt;post-&gt;find($id);\n        if($post) {\n            return view('posts\/show', compact('post'));\n        }\n        else {\n            return redirect()-&gt;to('\/posts');\n        }\n    }\n\n    \/**\n     * Return a new resource object, with default properties\n     *\n     * @return mixed\n     *\/\n    public function new()\n    {\n        return view('posts\/create');\n    }\n\n    \/**\n     * Create a new resource object, from \"posted\" parameters\n     *\n     * @return mixed\n     *\/\n    public function create()\n    {\n        $inputs = $this-&gt;validate([\n            'title' =&gt; 'required|min_length[5]',\n            'description' =&gt; 'required|min_length[5]',\n        ]);\n\n        if (!$inputs) {\n            return view('posts\/create', [\n                'validation' =&gt; $this-&gt;validator\n            ]);\n        }\n\n        $this-&gt;post-&gt;save([\n            'title' =&gt; $this-&gt;request-&gt;getVar('title'),\n            'description'  =&gt; $this-&gt;request-&gt;getVar('description')\n        ]);\n        session()-&gt;setFlashdata('success', 'Success! post created.');\n        return redirect()-&gt;to(site_url('\/posts'));\n    }\n\n    \/**\n     * Return the editable properties of a resource object\n     *\n     * @return mixed\n     *\/\n    public function edit($id = null)\n    {\n        $post = $this-&gt;post-&gt;find($id);\n        if($post) {\n            return view('posts\/edit', compact('post'));\n        }\n        else {\n            session()-&gt;setFlashdata('failed', 'Alert! no post found.');\n            return redirect()-&gt;to('\/posts');\n        }\n    }\n\n    \/**\n     * Add or update a model resource, from \"posted\" properties\n     *\n     * @return mixed\n     *\/\n    public function update($id = null)\n    {\n        $inputs = $this-&gt;validate([\n            'title' =&gt; 'required|min_length[5]',\n            'description' =&gt; 'required|min_length[5]',\n        ]);\n\n        if (!$inputs) {\n            return view('posts\/create', [\n                'validation' =&gt; $this-&gt;validator\n            ]);\n        }\n\n        $this-&gt;post-&gt;save([\n            'id' =&gt; $id,\n            'title' =&gt; $this-&gt;request-&gt;getVar('title'),\n            'description'  =&gt; $this-&gt;request-&gt;getVar('description')\n        ]);\n        session()-&gt;setFlashdata('success', 'Success! post updated.');\n        return redirect()-&gt;to(base_url('\/posts'));\n    }\n\n    \/**\n     * Delete the designated resource object from the model\n     *\n     * @return mixed\n     *\/\n    public function delete($id = null)\n    {\n        $this-&gt;post-&gt;delete($id);\n        session()-&gt;setFlashdata('success', 'Success! post deleted.');\n        return redirect()-&gt;to(base_url('\/posts'));\n    }\n}<\/code><\/pre>\n\n\n\n<p>Let me explain each and every functions used inside this controller.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explanation_of_PostController\"><\/span><strong>Explanation of PostController<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Namespace\"><\/span><strong>Namespace<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li>At very first, in the space area, I have imported the <strong>Post<\/strong> model and the <strong>ResourceController<\/strong>.<\/li><li>In the next line, the controller class extended the <code>ResourceController<\/code>. In this class, all the <strong>CRUD<\/strong> methods are defined.<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Constructor\"><\/span><strong>Constructor<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li>Next, I have declared a private variable and called it inside the constructor method. Here, in the constructor, I have used the helper functions to use it. Like <strong>form helper<\/strong> is used here for the form-related operations like validation, etc. Similarly, the <strong>URL<\/strong> and <strong>session<\/strong> helper for creating URL and populating flash message. Also, I created the object of the Post model. So, that we can use it further and it will not require to instantiate again.<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"CRUD_Methods\"><\/span><strong>CRUD Methods<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li>In the <strong>index()<\/strong> method, I fetched all the posts using the Post model and returned the data to the <strong>index view<\/strong>. (We will create this view in the next step).<\/li><li>The <strong>show() <\/strong>method is used to fetch the single post on the basis of the post id. If the post will found then it will return to the <strong>show<\/strong> view.<\/li><li> The <strong>new()<\/strong> method is just for loading the create post view.<\/li><li>In the <strong>create()<\/strong> method, firstly, I have set the validation rules to check the <strong>title<\/strong> and <strong>description<\/strong> of the post. If the data is not validated it will return to the same view with the validation error message. In the view, we will display this validation error message. If the inputs are validated, then using the model instance, I have stored it in the table and set the success message to the session flash data. After that redirected to the posts listing.<\/li><li>The <strong>edit()<\/strong> method is used to get the post on the basis of post id and will return to the <strong>edit<\/strong> view. If the post will not found, it will redirect to the posts listing with the flash message.<\/li><li>In the <strong>update()<\/strong> method, I used the same validation as I did in the <strong>create()<\/strong> method. After the validation success, I have updated the post on the basis of post id. After the update, it will set a flash message in the session and return to the posts listing.<\/li><li>Lastly, the <strong>delete() <\/strong>function is used to delete the post on the basis of the post id. If the post deleted, it will set the flash message and return to the posts listing.<\/li><\/ol>\n\n\n\n<p>That&#8217;s all for the CRUD methods in the PostController.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Routes_For_CRUD_in_CodeIgniter_4\"><\/span><strong>Create Routes For CRUD in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating the routes in CodeIgniter, you have to navigate to the <strong>Config\/routes.php<\/strong> file. Just put the below routes in the route definitions section.<\/p>\n\n\n\n<pre title=\"routes.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$routes-&gt;get('posts', 'PostController::index');\n$routes-&gt;get('posts\/new', 'PostController::new');\n$routes-&gt;post('posts', 'PostController::create');\n$routes-&gt;get('posts\/(:num)', 'PostController::show\/$1');\n$routes-&gt;get('posts\/edit\/(:num)', 'PostController::edit\/$1');\n$routes-&gt;put('posts\/(:num)', 'PostController::update\/$1');\n$routes-&gt;delete('posts\/(:num)', 'PostController::delete\/$1');<\/code><\/pre>\n\n\n\n<p>After creating the routes, we will create the views.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Views_For_Crud_Application_in_CodeIgniter_4\"><\/span><strong>Create Views For Crud Application in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Go to the <strong>app\/Views<\/strong> directory and create two sub-directories there.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>layouts<\/strong><\/li><li><strong>posts<\/strong><\/li><\/ol>\n\n\n\n<p>Now, inside the layouts folder, create a view named <strong>master.php<\/strong>. Actually, I am going to use Bootstrap 5 for some basic design. So, I will use here the master view concept to extend the same layout in every views. It will save our time and code.<\/p>\n\n\n\n<p>After that inside the <strong>posts<\/strong> directory create these views-<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>create.php<\/strong><\/li><li><strong>edit.php<\/strong><\/li><li><strong>index.php<\/strong><\/li><li><strong>show.php<\/strong><\/li><\/ol>\n\n\n\n<p>Your views structure should look like this.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>layouts <\/strong><ul><li><strong>master.php<\/strong><\/li><\/ul><\/li><li><strong>posts<\/strong><ul><li><strong>create.php<\/strong><\/li><li><strong>edit.php<\/strong><\/li><li><strong>index.php<\/strong><\/li><li><strong>show.php<\/strong><\/li><\/ul><\/li><\/ul>\n\n\n\n<p>You can see in the below screenshot.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"754\" height=\"295\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-13-36.png?resize=754%2C295&#038;ssl=1\" alt=\"Views Structure For CodeIgniter 4 CRUD\" class=\"wp-image-5609\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-13-36.png?w=754&amp;ssl=1 754w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-13-36.png?resize=300%2C117&amp;ssl=1 300w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><figcaption><strong>Views Structure For CodeIgniter 4 CRUD<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Render_Data_on_View_in_CodeIgniter_4\"><\/span><strong>Render Data on View in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We will start from the master view. So, just add the below code there.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Master_View\"><\/span><strong>Master View<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre title=\"master.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;CodeIgniter 4 CRUD Application | &lt;?= $this-&gt;renderSection('title') ?&gt;&lt;\/title&gt;\n    \n    &lt;!-- bootstrap 5 CSS --&gt;\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.0.0-beta3\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-eOJMYsd53ii+scO\/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6\" crossorigin=\"anonymous\"&gt;\n\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap-icons@1.4.1\/font\/bootstrap-icons.css\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div class=\"container-fluid py-4\"&gt;\n    &lt;h4 class=\"text-center fw-bold\"&gt;CodeIgniter 4 CRUD Application - Programming Fields&lt;\/h4&gt;\n    &lt;?= $this-&gt;renderSection('content') ?&gt;\n&lt;\/div&gt;\n\n&lt;!-- bootstrap 5 bundle JS --&gt;\n&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.0.0-beta3\/dist\/js\/bootstrap.bundle.min.js\" integrity=\"sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>In the master view, I have used the <strong>renderSection() <\/strong>function. It will create a placeholder for putting content dynamically in the view.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Post_View\"><\/span><strong>Create Post View<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre title=\"create.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?= $this-&gt;extend('layouts\/master');\n$this-&gt;section('title') ?&gt; Create Post &lt;?= $this-&gt;endSection() ?&gt;\n&lt;?= $this-&gt;section('content') ?&gt;\n\n&lt;div class=\"container\"&gt;\n&lt;?php $validation = \\Config\\Services::validation(); ?&gt;\n    &lt;div class=\"row py-4\"&gt;\n        &lt;div class=\"col-xl-12 text-end\"&gt;\n            &lt;a href=\"&lt;?= base_url('posts') ?&gt;\" class=\"btn btn-primary\"&gt;Back to Posts&lt;\/a&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n\n    &lt;div class=\"row\"&gt;\n        &lt;div class=\"col-xl-6 m-auto\"&gt;\n            &lt;form method=\"POST\" action=\"&lt;?= base_url('posts') ?&gt;\"&gt;\n                &lt;?= csrf_field() ?&gt;\n\n                &lt;div class=\"card shadow\"&gt;\n                    &lt;div class=\"card-header\"&gt;\n                        &lt;h5 class=\"card-title\"&gt;Create Post&lt;\/h5&gt;\n                    &lt;\/div&gt;\n                    &lt;div class=\"card-body p-4\"&gt;\n                        &lt;div class=\"form-group mb-3 has-validation\"&gt;\n                            &lt;label class=\"form-label\"&gt;Post Title&lt;\/label&gt;\n                            &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('title')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"title\" placeholder=\"Post Title\" value=\"&lt;?php echo set_value('title'); ?&gt; \"\/&gt;\n                            &lt;?php if ($validation-&gt;getError('title')): ?&gt;\n                                &lt;div class=\"invalid-feedback\"&gt;\n                                    &lt;?= $validation-&gt;getError('title') ?&gt;\n                                &lt;\/div&gt;                                \n                            &lt;?php endif; ?&gt;\n                        &lt;\/div&gt;\n                        &lt;div class=\"form-group\"&gt;\n                            &lt;label class=\"form-label\"&gt;Description&lt;\/label&gt;\n                            &lt;textarea class=\"form-control &lt;?php if($validation-&gt;getError('description')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"description\" placeholder=\"Description\"&gt;&lt;?php echo set_value('description'); ?&gt;&lt;\/textarea&gt;\n                                &lt;?php if ($validation-&gt;getError('description')): ?&gt;\n                                &lt;div class=\"invalid-feedback\"&gt;\n                                    &lt;?= $validation-&gt;getError('description') ?&gt;\n                                &lt;\/div&gt;                                \n                            &lt;?php endif; ?&gt;\n                        &lt;\/div&gt;\n                    &lt;\/div&gt;\n\n                    &lt;div class=\"card-footer\"&gt;\n                        &lt;button type=\"submit\" class=\"btn btn-success\"&gt;Save&lt;\/button&gt;\n                    &lt;\/div&gt;\n                &lt;\/div&gt;\n            &lt;\/form&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n\n&lt;?= $this-&gt;endSection() ?&gt;<\/code><\/pre>\n\n\n\n<p>In the create view, I populated the form validation error message and created the form to submit the post.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Post_Edit_View\"><\/span><strong>Post Edit View <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre title=\"edit.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?= $this-&gt;extend('layouts\/master');\n$this-&gt;section('title') ?&gt; Create Post &lt;?= $this-&gt;endSection() ?&gt;\n&lt;?= $this-&gt;section('content') ?&gt;\n\n&lt;div class=\"container\"&gt;\n&lt;?php $validation = \\Config\\Services::validation(); ?&gt;\n\n    &lt;div class=\"row py-4\"&gt;\n        &lt;div class=\"col-xl-12 text-end\"&gt;\n            &lt;a href=\"&lt;?= base_url('posts') ?&gt;\" class=\"btn btn-primary\"&gt;Back to Posts&lt;\/a&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n\n    &lt;div class=\"row\"&gt;\n        &lt;div class=\"col-xl-6 m-auto\"&gt;\n            &lt;form method=\"POST\" action=\"&lt;?= base_url('posts\/'.$post['id']) ?&gt;\"&gt;\n                &lt;?= csrf_field() ?&gt;\n                &lt;input type=\"hidden\" name=\"_method\" value=\"PUT\"\/&gt;\n\n                &lt;div class=\"card shadow\"&gt;\n                    &lt;div class=\"card-header\"&gt;\n                        &lt;h5 class=\"card-title\"&gt;Update Post&lt;\/h5&gt;\n                    &lt;\/div&gt;\n                    &lt;div class=\"card-body p-4\"&gt;\n                        &lt;div class=\"form-group mb-3 has-validation\"&gt;\n                            &lt;label class=\"form-label\"&gt;Post Title&lt;\/label&gt;\n                            &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('title')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"title\" placeholder=\"Post Title\" value=\"&lt;?php if($post['title']): echo $post['title']; else: set_value('title'); endif ?&gt;\"\/&gt;\n                            &lt;?php if ($validation-&gt;getError('title')): ?&gt;\n                                &lt;div class=\"invalid-feedback\"&gt;\n                                    &lt;?= $validation-&gt;getError('title') ?&gt;\n                                &lt;\/div&gt;                                \n                            &lt;?php endif; ?&gt;\n                        &lt;\/div&gt;\n                        &lt;div class=\"form-group\"&gt;\n                            &lt;label class=\"form-label\"&gt;Description&lt;\/label&gt;\n                            &lt;textarea class=\"form-control &lt;?php if($validation-&gt;getError('description')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"description\" placeholder=\"Description\"&gt;&lt;?php if($post['description']): echo $post['description']; else: set_value('description'); endif ?&gt;&lt;\/textarea&gt;\n                                &lt;?php if ($validation-&gt;getError('description')): ?&gt;\n                                &lt;div class=\"invalid-feedback\"&gt;\n                                    &lt;?= $validation-&gt;getError('description') ?&gt;\n                                &lt;\/div&gt;                                \n                            &lt;?php endif; ?&gt;\n                        &lt;\/div&gt;\n                    &lt;\/div&gt;\n\n                    &lt;div class=\"card-footer\"&gt;\n                        &lt;button type=\"submit\" class=\"btn btn-success\"&gt;Update&lt;\/button&gt;\n                    &lt;\/div&gt;\n                &lt;\/div&gt;\n            &lt;\/form&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n\n&lt;?= $this-&gt;endSection() ?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Index_view_to_display_all_posts\"><\/span><strong>Index view to display all posts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre title=\"index.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?= $this-&gt;extend('layouts\/master');\n$this-&gt;section('title') ?&gt; Posts &lt;?= $this-&gt;endSection() ?&gt;\n&lt;?= $this-&gt;section('content'); ?&gt;\n   \n   &lt;div class=\"container\"&gt;\n        &lt;div class=\"row py-4\"&gt;\n            &lt;div class=\"col-xl-12 text-end\"&gt;\n                &lt;a href=\"&lt;?= base_url('posts\/new') ?&gt;\" class=\"btn btn-primary\"&gt;Add Post&lt;\/a&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n\n            &lt;div class=\"row py-2\"&gt;\n                &lt;div class=\"col-xl-12\"&gt;\n                    &lt;?php\n                        if(session()-&gt;getFlashdata('success')):?&gt;\n                            &lt;div class=\"alert alert-success alert-dismissible\"&gt;\n                                &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\"&gt;&amp;times;&lt;\/button&gt;\n                                &lt;?php echo session()-&gt;getFlashdata('success') ?&gt;\n                            &lt;\/div&gt;\n                        &lt;?php elseif(session()-&gt;getFlashdata('failed')):?&gt;\n                            &lt;div class=\"alert alert-danger alert-dismissible\"&gt;\n                                &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\"&gt;&amp;times;&lt;\/button&gt;\n                                &lt;?php echo session()-&gt;getFlashdata('failed') ?&gt;\n                            &lt;\/div&gt;\n                    &lt;?php endif; ?&gt;\n\n                    &lt;div class=\"card\"&gt;\n                        &lt;div class=\"card-header\"&gt;\n                            &lt;h5 class=\"card-title\"&gt;Posts&lt;\/h5&gt;\n                        &lt;\/div&gt;\n                        &lt;div class=\"card-body\"&gt;\n                            &lt;table class=\"table table-striped\"&gt;\n                                &lt;thead&gt;\n                                    &lt;tr&gt;\n                                        &lt;th&gt;Id&lt;\/th&gt;\n                                        &lt;th&gt;Title&lt;\/th&gt;\n                                        &lt;th&gt;Description&lt;\/th&gt;\n                                        &lt;th&gt;Action&lt;\/th&gt;\n                                    &lt;\/tr&gt;\n                                &lt;\/thead&gt;\n                                &lt;tbody&gt;\n                                    &lt;?php \n                                        if (count($posts) &gt; 0):\n                                            foreach ($posts as $post): ?&gt;\n                                                &lt;tr&gt;\n                                                    &lt;td&gt;&lt;?= $post['id'] ?&gt;&lt;\/td&gt;\n                                                    &lt;td&gt;&lt;?= $post['title'] ?&gt;&lt;\/td&gt;\n                                                    &lt;td&gt;&lt;?= $post['description'] ?&gt;&lt;\/td&gt;\n                                                    &lt;td class=\"d-flex\"&gt;\n                                                        &lt;a href=\"&lt;?= base_url('posts\/'.$post['id']) ?&gt;\" class=\"btn btn-sm btn-info mx-1\" title=\"Show\"&gt;&lt;i class=\"bi bi-info-square\"&gt;&lt;\/i&gt;&lt;\/a&gt;\n                                                        &lt;a href=\"&lt;?= base_url('posts\/edit\/'.$post['id']) ?&gt;\" class=\"btn btn-sm btn-success mx-1\" title=\"Edit\"&gt;&lt;i class=\"bi bi-pencil-square\"&gt;&lt;\/i&gt;&lt;\/a&gt;\n                                                        &lt;form class=\"display-none\" method=\"post\" action=\"&lt;?= base_url('posts\/'.$post['id'])?&gt;\" id=\"postDeleteForm&lt;?=$post['id']?&gt;\"&gt;\n                                                        &lt;input type=\"hidden\" name=\"_method\" value=\"DELETE\"\/&gt;\n                                                            &lt;a href=\"javascript:void(0)\" onclick=\"deletePost('postDeleteForm&lt;?=$post['id']?&gt;')\" class=\"btn btn-sm btn-danger\" title=\"Delete\"&gt;&lt;i class=\"bi bi-trash\"&gt;&lt;\/i&gt;&lt;\/a&gt;\n                                                        &lt;\/form&gt;\n                                                    &lt;\/td&gt;\n                                                &lt;\/tr&gt;\n                                            &lt;?php endforeach;\n                                        else: ?&gt;\n                                            &lt;tr rowspan=\"1\"&gt;\n                                                &lt;td colspan=\"4\"&gt;\n                                                    &lt;h6 class=\"text-danger text-center\"&gt;No post found&lt;\/h6&gt;\n                                                &lt;\/td&gt;\n                                            &lt;\/tr&gt;\n                                        &lt;?php endif ?&gt;\n                                &lt;\/tbody&gt;\n                            &lt;\/table&gt;\n                        &lt;\/div&gt;\n                    &lt;\/div&gt;\n                &lt;\/div&gt;\n            &lt;\/div&gt;\n   &lt;\/div&gt;\n\n   &lt;script&gt;\n    function deletePost(formId) {\n        var confirm = window.confirm('Do you want to delete this post?');\n        if(confirm == true) {\n            document.getElementById(formId).submit();\n        }\n    }\n&lt;\/script&gt;\n&lt;?= $this-&gt;endSection(); ?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Show_Post_View\"><\/span><strong>Show Post View<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre title=\"show.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?= $this-&gt;extend('layouts\/master');\n$this-&gt;section('title') ?&gt; Show Post &lt;?= $this-&gt;endSection() ?&gt;\n&lt;?= $this-&gt;section('content') ?&gt;\n\n&lt;div class=\"container\"&gt;\n    &lt;div class=\"row py-4\"&gt;\n        &lt;div class=\"col-xl-12 text-end\"&gt;\n            &lt;a href=\"&lt;?= base_url('posts') ?&gt;\" class=\"btn btn-primary\"&gt;Back to Posts&lt;\/a&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n\n    &lt;div class=\"row\"&gt;\n        &lt;div class=\"col-xl-6 m-auto\"&gt;\n            &lt;div class=\"card shadow\"&gt;\n                &lt;div class=\"card-header\"&gt;\n                    &lt;h5 class=\"card-title\"&gt;Show Post&lt;\/h5&gt;\n                &lt;\/div&gt;\n                &lt;div class=\"card-body p-4\"&gt;\n                    &lt;div class=\"form-group mb-3 has-validation\"&gt;\n                        &lt;label class=\"form-label\"&gt;Post Title&lt;\/label&gt;\n                        &lt;input type=\"text\" class=\"form-control\" disabled placeholder=\"Post Title\" value=\"&lt;?php echo trim($post['title']);?&gt;\"\/&gt;\n                    &lt;\/div&gt;\n                    &lt;div class=\"form-group\"&gt;\n                        &lt;label class=\"form-label\"&gt;Description&lt;\/label&gt;\n                        &lt;textarea class=\"form-control\" disabled name=\"description\" placeholder=\"Description\"&gt;&lt;?php echo trim($post['description']);?&gt;&lt;\/textarea&gt;\n                    &lt;\/div&gt;\n                &lt;\/div&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n\n&lt;?= $this-&gt;endSection() ?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"CRUD_Operation_Results\"><\/span><strong>CRUD Operation Results<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now, it&#8217;s time to see the results for the CRUD operations. So, run the application using the command line using the php spark.<\/p>\n\n\n\n<pre title=\"run codeigniter 4 application\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark serve<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"109\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-27-21.png?resize=1024%2C109&#038;ssl=1\" alt=\"CodeIgniter 4 Application Running\" class=\"wp-image-5612\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-27-21.png?resize=1024%2C109&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-27-21.png?resize=300%2C32&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-27-21.png?resize=768%2C82&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-27-21.png?w=1073&amp;ssl=1 1073w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>CodeIgniter 4 Application Started<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Open the browser, and hit the above URL that is &#8211; <code>http:\/\/localhost:8080<\/code> to see the result.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"466\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-28-56.png?resize=1024%2C466&#038;ssl=1\" alt=\"CodeIgniter 4 Homepage\" class=\"wp-image-5613\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-28-56.png?resize=1024%2C466&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-28-56.png?resize=300%2C136&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-28-56.png?resize=768%2C349&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-22-07-28-56.png?w=1258&amp;ssl=1 1258w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>CodeIgniter 4 Homepage<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>So, let&#8217;s navigate to the posts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Posts_Listing\"><\/span><strong>Posts Listing<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Currently, we have no posts in the table. So, firstly, we will create a post. Just click on <strong>Add Post<\/strong> button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"400\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-22.png?resize=1024%2C400&#038;ssl=1\" alt=\"Posts Listing - No post found\" class=\"wp-image-5614\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-22.png?resize=1024%2C400&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-22.png?resize=300%2C117&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-22.png?resize=768%2C300&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-22.png?w=1271&amp;ssl=1 1271w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Posts Listing &#8211; No post found<\/strong><\/figcaption><\/figure>\n\n\n\n<p>It will open a new page to create a post.<\/p>\n\n\n\n<p><strong>Create New Post<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"429\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-42.png?resize=1024%2C429&#038;ssl=1\" alt=\"Create New Post\" class=\"wp-image-5615\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-42.png?resize=1024%2C429&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-42.png?resize=300%2C126&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-42.png?resize=768%2C321&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-42.png?w=1264&amp;ssl=1 1264w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Create New Post<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>As, we used the form validation, so you will see the validation error message.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"483\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-55.png?resize=1024%2C483&#038;ssl=1\" alt=\"Validation Error Message In CodeIgniter 4 Using Bootstrap 5\" class=\"wp-image-5616\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-55.png?resize=1024%2C483&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-55.png?resize=300%2C141&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-55.png?resize=768%2C362&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-25-55.png?w=1252&amp;ssl=1 1252w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Validation Error Message In CodeIgniter 4 Using Bootstrap 5<\/strong><\/figcaption><\/figure>\n\n\n\n<p>After creating the post, you will be redirected to the post listing with a success message. Because we used session flash data to set the message. Here, I created few posts so that you can see the list.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"513\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-28-54.png?resize=1024%2C513&#038;ssl=1\" alt=\"Display Posts\" class=\"wp-image-5617\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-28-54.png?resize=1024%2C513&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-28-54.png?resize=300%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-28-54.png?resize=768%2C385&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-28-54.png?w=1198&amp;ssl=1 1198w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Display All Posts<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Display_Single_Post_Result\"><\/span><strong>Display Single Post Result<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>When you will click on show post action, it will open the new view with the post detail. This is just to show the post, so the form is disabled.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"463\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-20.png?resize=1024%2C463&#038;ssl=1\" alt=\"Display Single Post - Post Detail\" class=\"wp-image-5619\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-20.png?resize=1024%2C463&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-20.png?resize=300%2C136&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-20.png?resize=768%2C347&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-20.png?w=1161&amp;ssl=1 1161w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Display Single Post &#8211; Post Detail<\/strong><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"EditUpdate_Post_Result\"><\/span>Edit\/<strong>Update Post Result<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>When you will click on the edit action, it will redirect you to the edit post. Here, the form validation is already set so you will see the validation error message.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"481\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-35.png?resize=1024%2C481&#038;ssl=1\" alt=\"Edit\/Update Post\" class=\"wp-image-5618\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-35.png?resize=1024%2C481&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-35.png?resize=300%2C141&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-35.png?resize=768%2C361&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-29-35.png?w=1154&amp;ssl=1 1154w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Edit\/Update Post<\/strong><\/figcaption><\/figure>\n\n\n\n<p>After updating the post, you will be redirected back to the lists with message.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"506\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-05-1.png?resize=1024%2C506&#038;ssl=1\" alt=\"Post Updated with Success message\" class=\"wp-image-5623\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-05-1.png?resize=1024%2C506&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-05-1.png?resize=300%2C148&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-05-1.png?resize=768%2C380&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-05-1.png?w=1189&amp;ssl=1 1189w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Post Updated with Success Message<\/strong><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Delete_Post_Result\"><\/span><strong>Delete Post Result<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>When you will click on the delete post option, it will ask you the confirmation. I have used the javascript for it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"450\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-32-1.png?resize=1024%2C450&#038;ssl=1\" alt=\"Post Delete Confirmation\" class=\"wp-image-5624\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-32-1.png?resize=1024%2C450&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-32-1.png?resize=300%2C132&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-32-1.png?resize=768%2C338&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-32-1.png?w=1199&amp;ssl=1 1199w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Post Delete Confirmation<\/strong><\/figcaption><\/figure>\n\n\n\n<p>After confirmation, it will delete the post.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"470\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-46.png?resize=1024%2C470&#038;ssl=1\" alt=\"Post Deleted\" class=\"wp-image-5625\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-46.png?resize=1024%2C470&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-46.png?resize=300%2C138&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-46.png?resize=768%2C353&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/Screenshot-from-2021-04-21-22-30-46.png?w=1189&amp;ssl=1 1189w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Post Deleted<\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>The CodeIgniter 4 is launched with the updated features over the CodeIgniter 3. Now, it is a little bit similar to the Laravel framework. You can create the database schema using the migrations in CodeIgniter 4. It will work like version control of the database schema. Every time, when you will update the schema, you [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":5629,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1977],"tags":[2037,2034,2035,2033],"yst_prominent_words":[134,105,133],"class_list":{"0":"post-5570","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-codeigniter-4","8":"tag-codeigniter-4-create-controller","9":"tag-codeigniter-4-crud","10":"tag-codeigniter-create-model","11":"tag-crud-application-in-codeigniter","12":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/04\/create-crud.png?fit=2240%2C1260&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/5570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=5570"}],"version-history":[{"count":0,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/5570\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/5629"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=5570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=5570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=5570"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=5570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}