MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to install Laravel Application & sample CRUD.

Since its initial release in 2011, Laravel has experienced exponential growth. In 2015, it became the most starred PHP framework on GitHub and rose to the go-to framework for people all over the world.

Before using Laravel, make sure you have Composer installed in your machine. You have to install the composer https://getcomposer.org/.

Step 1:

Windows Explorer xampp htdocs folder with right-click menu showing Git Bash Here

composer create-project –prefer-dist laravel/laravel blog

Step 2:

Git Bash in htdocs running composer create-project --prefer-dist laravel/laravel blog

It will take time for install this blog project.

Step 3:

Git Bash output of Laravel install finishing with package discovery and application key set

Step 4:

After installing, go to your project root folder and type PHP artisan serve in the terminal, your project URL might look like this

Git Bash in the blog project folder running the php artisan serve command

Step 5:

Php artisan serve output: Laravel development server started at http://127.0.0.1:8000

Step 6: Edit .env file for database configuration.

Laravel .env database settings for MySQL on 127.0.0.1:3306 with database laravel54

If you have created the database in MySQL and you have provided correct credentials to the .env file, then you will see there are three tables generated in MySQL database.

Step 7: Create a controller file for our CRUD operations.

Command php artisan make:controller CRUDController --resource to create the CRUD controller

Step 8: Create a model file for CRUD operations.

Command php artisan make:model Crud -m to create the Crud model with a migration

Step 9: Edit the crud migration file and create the required fields for the database.

Migration file is located in blog > database > migrations folder.

Laravel create_cruds_table migration defining id, title, post and timestamps columns

Step 10:

Command php artisan migrate to run the Laravel migrations

Step 11: Create views for set up a form

Go to blog > resources > views . Locate into that folder and then create a master view called master.blade.php. A blade is a templating engine used by laravel.

Master.blade.php layout with HTML head, app.css link and @yield('content')

Step 12

Now create a new folder inside views directory called crud. Go inside that folder and create the following files.

  1. index.blade.php
  2. create.blade.php
  3. edit.blade.php

Step 13: Create a form in create.blade.php.

Create.blade.php Bootstrap form with Title input, Post textarea and submit button

Step 14: Setup a route for request handling.

Go to the routes > web.php

Routes/web.php code adding Route::resource('crud', 'CRUDController')

Here we have added resource route, and all the functions reside in app > Http > controllers > CRUDController

Step 15: Edit CRUDController File.

CRUDController create method returning the crud.create view

Here we have to return the view when the request hits the route; it will be redirected to this controller’s create method. Our view should be accessible via the following URL the form.

Step 16: Add CSRF token and set the post route of the form.

We have put the {{csrf_field()}} in the form so that malicious attacks can not harm our web application.

Step 17: Code the store function and use the Crud model to insert the data.

CRUDController code with create, store saving a new Crud record, and empty show method

Here we need to create a protected field called $fillable in the Crud model. Otherwise, it will throw a mass assignment exception.

Crud.php model setting protected $fillable to title and post

Now, when you fill the title and post field in our form and submit the form, the new entry will be added to the database. We can confirm it by following steps.

Step 17: Code index() function in the CRUDController File.

CRUDController store method saving title and post, then redirecting to /crud

Step 18: Need to update index.blade.php.

Index.blade.php striped table looping over $cruds to list ID, Title and Post

You will see a table that has the id, title, and post-column with their respective data.

Step 19: Add Edit and Delete Button in the index.blade.php.

Index.blade.php table with added Edit and Delete action buttons for each row

Step 20: Create an edit function to pass the data to the edit view.

CRUDController edit method finding the Crud by id and returning the crud.edit view

Step 21: Create an edit view.

Edit.blade.php form with CSRF field, hidden PATCH _method and an Update button

Here we have used one more hidden field called _method, which will be a PATCH request to the server so that we can update the data very quickly.

Step 22: Code update() in the CRUDController.

CRUDController update method saving the new title and post, then redirecting to /crud

Step 23: Create a delete form to delete the data.

Laravel index.blade.php table listing posts with Edit buttons and a Delete form using a hidden DELETE _method

Step 24: Code the destroy() method in the CRUDController.

CRUDController destroy() method that finds the Crud record, deletes it and redirects to /crud

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
sam
MotoShare.in is your go-to platform for adventure and exploration. Rent premium bikes for epic journeys or simple scooters for your daily errands—all with the MotoShare.in advantage of affordability and ease.

Related Posts

How to Delete Multiple Records using Checkbox in Laravel?

It’s almost need to give feature for remove multiple records using checkbox, if you are developing e-commerce application or any big web application then you must give…

Read More

CRUD Operations in Laravel PHP Framework

Step 1 – 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…

Read More

How to merge two or multiple tables to each other in the Laravel PHP Framework? (Part-3)

Part-1 Part-2 Now, we’ll create the 2nd table State to merge with the Country table. Step 1. Create child file resources/views/State folder with name create.blade.php. Step 2. Create edit.blade.php file. In this…

Read More

How to merge two or multiple tables to each other in the Laravel PHP Framework? (Part-2)

How to merge two or multiple tables to each other in the Laravel PHP Framework? Part-1 Part-3 Step 1. Go to app/Country.php. Now, we need to list…

Read More

How to create captcha code in Laravel 5.8

Step:1composer create-project –prefer-dist laravel/laravel captcha “5.8.*” Step:2 we have to add mews Captcha package.composer require mews/captcha Step:3 open config/app.php file and add service provider and alias. Step:4 routes/web.php paste…

Read More

Todo Application in Laravel

1.Create Project in Laravel Composer create-project laravel/laravel todolist 2.Create CRUD Controller php artisan make:controller TodosController –resource 3.Route setup of CRUD Controller routes/web.php 4.Create Database todolist localhost/phpmyadmin create…

Read More