{"id":18470,"date":"2020-09-10T11:50:59","date_gmt":"2020-09-10T11:50:59","guid":{"rendered":"http:\/\/www.devopsschool.com\/blog\/?p=18470"},"modified":"2022-04-28T09:15:13","modified_gmt":"2022-04-28T09:15:13","slug":"crud-operations-in-laravel-php-framework","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/crud-operations-in-laravel-php-framework\/","title":{"rendered":"CRUD Operations in Laravel PHP Framework"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Step 1 &#8211; Download Laravel 5.8<\/h3>\n\n\n\n<p>In first step you have to download Laravel 5.8 version. For this you have to go to command prompt, in which first you have go to your folder path in which you want to download Laravel 5.8. After this you have to run &#8220;composer&#8221; command, because all Laravel depository handle by composer. After run &#8220;composer&#8221; command. You have to run following command.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">composer create-project laravel\/laravel=5.8 crud --prefer-dist<\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/1.png\" alt=\"\" class=\"wp-image-18482\"\/><figcaption>Above command will make&nbsp;<strong>crud<\/strong>&nbsp;folder and under this folder it will download Laravel 5.8.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2 &#8211; Mysql Database connection Laravel 5.8<\/h3>\n\n\n\n<p>Once you have download Laravel 5.8, so now first we want to make Mysql database connection from Laravel 5.8. You can make Mysql database connection from two way.<\/p>\n\n\n\n<p>In first way you have to find&nbsp;<strong>.env<\/strong>&nbsp;file in your Laravel 5.8 folder. Open that file and under you have to define your mysql database configuration like below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/2-2.png\" alt=\"\" class=\"wp-image-18490\"\/><\/figure>\n\n\n\n<p>In second way, you have to open&nbsp;<strong>config\/database.php<\/strong>&nbsp;file, and under this file you can also define your mysql database configuration.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/3-3.png\" alt=\"\" class=\"wp-image-18489\"\/><figcaption>After defining above details you can make Mysql database connection from your Laravel 5.8 application.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3 &#8211; Migrate Table from Laravel 5.8 to Mysql Database<\/h3>\n\n\n\n<p>In Laravel framework, you have facility from make table in Mysql database from your Laravel application. For this first you have to create migration file in your Laravel folder. For this you have to write following command in your command prompt.<\/p>\n\n\n\nphp artisan make:migration create_crud_table &#8211;create=crud\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/4-5.png\" alt=\"\" class=\"wp-image-18575\"\/><\/figure>\n\n\n\n<p>This command will create migration file in&nbsp;<strong>database\/migrations<\/strong>&nbsp;folder. In this file we have to define table column which we want to create in table. Below you can find migration file in which we have define table column.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/5-5.png\" alt=\"\" class=\"wp-image-18577\"\/><\/figure>\n\n\n\n<p>Now we want to migrate this table definition from this Laravel application to mysql database. For this we have write following command in command prompt. This command will make crud table in mysql database for perform CRUD operation from Laravel 5.8 application.<\/p>\n\n\n\nphp artisan migrate\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/6-1.png\" alt=\"\" class=\"wp-image-18494\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4 &#8211; Create Model file in Laravel 5.8<\/h3>\n\n\n\n<p>In this we will seen how can we make model file in Laravel 5.8. This class file mainly used for do database related operation in controller class. For create model files we have to write following command in command prompt.<\/p>\n\n\n\nphp artisan make:model Crud -m\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/6-2.png\" alt=\"\" class=\"wp-image-18580\"\/><\/figure>\n\n\n\n<p>This command will make&nbsp;<strong>Crud.php<\/strong>&nbsp;model file in&nbsp;<strong>app<\/strong>&nbsp;folder. In this file we have to define table column name which you can see below source code of&nbsp;<strong>Crud.php<\/strong>&nbsp;file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/8-2.png\" alt=\"\" class=\"wp-image-18583\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5 &#8211; Create Controllers in Laravel 5.8<\/h4>\n\n\n\n<p>Controllers files mainly used for handle http request in Laravel 5.8 application. Here we want to create Laravel 5.8 crud controller. For this we have to go to command prompt and under this we have write following command.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/9-1.png\" alt=\"\" class=\"wp-image-18588\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6 &#8211; Set Route in Laravel 5.8<\/h3>\n\n\n\n<p>This is very useful step in Laravel 5.8, because here we want to set route of all CrudsController class method. For this we have to open to&nbsp;<strong>routes\/web.php<\/strong>&nbsp;file. In this file we have to write following code for set route of all method.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/11-1.png\" alt=\"\" class=\"wp-image-18594\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7 &#8211; Set Data in View File in Laravel 5.8<\/h3>\n\n\n\n<p>This is the last step in Laravel 5.8 Crud application, and in this step we have to set data in view file which has been store under&nbsp;<strong>resources\/views<\/strong>&nbsp;folder, because this view file has received data from controller method, so here we have to set data in view file. Below you can file all view file which has been used in Crud application, and you can also find how data has been set and how to make form in Laravel 5.8 view file.<\/p>\n\n\n\n<p><strong>resources\/views\/parent.blade.php<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/12-1.png\" alt=\"\" class=\"wp-image-18595\"\/><\/figure>\n\n\n\n<p>This command will make&nbsp;<strong>CrudsController.php<\/strong>&nbsp;file in&nbsp;<strong>app\/Http\/Controllers<\/strong>&nbsp;folder. Once you have open this file, then you can find all predefine method for do CRUD operation in this controller file. We have to just add code for do particular operation. Below you can find CRUD controller file code below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Controller&nbsp;<code>index()<\/code><\/h3>\n\n\n\n<p>Paste the following code into&nbsp;<code>index()<\/code>&nbsp;function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/10-3.png\" alt=\"\" class=\"wp-image-18592\"\/><\/figure>\n\n\n\n<p><strong>index() &#8211;&nbsp;<\/strong>In Laravel Crud controller index() method is a root method of crud controller, so when in browser we have enter base url with controller name, then it will called this index() method. Under this method we will write code for display data from mysql database. In this code it will first fetch data from crud table and store under $data variable, after this we want to make paginate link by using paginate() method in Laravel 5.8. For send data to view file, for this we have use view() method for send data to view file. Source code you can find under index() method.<\/p>\n\n\n\n<p><strong>resources\/views\/index.blade.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmendra150696\/a45d8e997f2742f15771dd0fb3b00158.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Controller&nbsp;<code>Create()<\/code><\/h3>\n\n\n\n<p>Now copy the below code and paste into&nbsp;<code>create()<\/code>&nbsp;function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/13-1.png\" alt=\"\" class=\"wp-image-18596\"\/><\/figure>\n\n\n\n<p><strong>create() &#8211;&nbsp;<\/strong>This method has been used for load&nbsp;<strong>create.blade.php<\/strong>&nbsp;file. In this file user can find form for insert new records and filling data insert data request will be send to store() method of CrudsController.php controller class.<\/p>\n\n\n\n<p><strong><strong>resources\/views\/create.blade.php<\/strong><\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmendra150696\/5c39479c3a0d6f6b5449f6db0ee14a6b.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Controller&nbsp;<code>store()<\/code><\/h3>\n\n\n\n<p><strong>store() &#8211;&nbsp;<\/strong>This method has received add or insert new records request received from create() method. In this method, it will perform two operation. One is for upload image file by using move() method and second is for insert records into mysql table by using model class. Before upload of image, it will rename image name. After successfully insert or add of data in mysql table from this Laravel 5.8 application, page will redirect to index() method with success message.<\/p>\n\n\n\n<p>To insert data into the database, paste the following code into store function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/13-2.png\" alt=\"\" class=\"wp-image-18601\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Controller&nbsp;<code>show()<\/code><\/h3>\n\n\n\n<p>To insert data into the database, paste the following code into show function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/14-1.png\" alt=\"\" class=\"wp-image-18603\"\/><\/figure>\n\n\n\n<p><strong>show() &#8211;&nbsp;<\/strong>This method in Crud controller has been used for fetch single data details based on on value of $id argument. For this it has been used findOrFail() method. After fetch details it will send to view.blade.php file.<\/p>\n\n\n\n<p><strong>resources\/views\/view.blade.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmendra150696\/f6386158384e238bb96b20e112498062.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">&nbsp;Controller&nbsp;<code>edit()<\/code><\/h3>\n\n\n\n<p>To insert data into the database, paste the following code into edit function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/16.png\" alt=\"\" class=\"wp-image-18608\"\/><\/figure>\n\n\n\n<p><strong>edit() &#8211;&nbsp;<\/strong>This method main function is fetch single data from Mysql database and load into edit or update form for make required changes.<\/p>\n\n\n\n<p><strong>resources\/views\/edit.blade.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmendra150696\/9db36c66d3e1dcd4661f6d4b09c69bee.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">&nbsp;Controller&nbsp;<code><strong>update<\/strong>()<\/code><\/h3>\n\n\n\n<p><strong>update() &#8211;&nbsp;<\/strong>Update method has received edit or update data request from edit(). This method has done two function like upload of profile image with update or edit mysql data in Laravel 5.8 framework.<\/p>\n\n\n\n<p>To insert data into the database, paste the following code into <strong>update<\/strong> function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/17.png\" alt=\"\" class=\"wp-image-18612\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">&nbsp;Controller&nbsp;<code><strong>delete<\/strong>()<\/code><\/h3>\n\n\n\n<p><strong>delete() &#8211;&nbsp;<\/strong>Delete() method mainly used for remove single or multiple data from Mysql Database. This is last operation Crud Operation in Laravel 5.8 Crud tutorial series.<\/p>\n\n\n\n<p>To insert data into the database, paste the following code into <strong>delete<\/strong> function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/18.png\" alt=\"\" class=\"wp-image-18613\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 8 &#8211; Run Laravel 5.8 Application<\/h4>\n\n\n\n<p>Lastly, we want to run Laravel 5.8 Crud application, for this we have to go to command prompt, and write following command.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/09\/19.png\" alt=\"\" class=\"wp-image-18614\"\/><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_97840\"><iframe loading=\"lazy\"  id=\"_ytid_52147\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_97840\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Step 1 &#8211; Download Laravel 5.8 In first step you have to download Laravel 5.8 version. For this you have to go to command prompt, in which first you have&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5201],"tags":[],"class_list":["post-18470","post","type-post","status-publish","format-standard","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/18470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=18470"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/18470\/revisions"}],"predecessor-version":[{"id":24540,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/18470\/revisions\/24540"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=18470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=18470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=18470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}