Skip to main content
Laravel 5.2 CRUD (Create, Read, Update, Delete ) Application

Laravel 5.2 CRUD Application

In this tutorial I am going to provide step by step guide to implement CREAT, READ, UPDATE and DELETE Operations using Laravel 5.2 PHP Framework. 

Development Platform:

Laravel Provides Homestead development environment, it’s my preferred option while working with laravel project, it has all required configuration ready to use for the framework, if you have’t heard about homestead just visit Laravel official website for download and installation. however you can also continue with your existing environment it can be LAMP, MAPM or WAMP just make sure with the following softwares and extensions.

  • PHP >= 5.5.9
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • Composer

If you have your server configuration ready to use then let’s get started with the first step to install Laravel framework.

Step 1: Install Laravel:

Open up the terminal and navigate cd to your server root directory and type in following command to install Laravel, application directory is depend on where your server root is located, for example LAMP is having it set to /var/www/.

let’s see what happen next!

Laravel Installing process

After Installing laravel successfully you should see crud-demo folder with laravel application, change the file permissions to be able read, write and execute to do that use following command, make sure you have provided correct path.

You can try to run newly installed laravel project on browser, in my case the URL is: http://crud-demo.dev:8888. it should show up laravel welcome screen.

Laravel-Welcome-Screen

 

Step 2: Database Connection Settings:

Create a database and change settings from .env file showing below, make sure to mention password if database user is having password set.

Step 3: Database migration:

Basically we are going to setup database table with laravel migration, use following command to create migration file from terminal and open it up in the editor. ( All migration files are located in database/migrations folder)

Update the Schema to specify table fields as showing below:

If you ready with the migration file use following to migrate the table.

You will get Migration table created successfully. message in your terminal.

Let’s go the next step to create Laravel Model for task table.

Step 4: Create Eloquent Model:

Open the Task.php file, it should be located in app/Task.php directory and add $fillable variable.

Step 5: Define Routes:

In Laravel we have to define routes to handle request and response,  route can be define in app/Http/routes.php file. open up the routes file and add following code to define routes for our CRUD operations.

Route::get('/', 'TaskController@index'); – To list task by default on home page.

Route::resource('tasks', 'TaskController'); – Resource route it will generate CRUD URI.

ex. http://crud-demo.dev:8888/ or we can use resource route that is http://crud-demo.dev:8888/tasks  both will work same.

We need to add our resource controller for tasks routes, go ahead and create controller using following command:

Controller are located in app/Http/Controllers directory, you open up the TaskController to view file it should match with below controller file:

TaskController.php:

Use following command to list and make sure you have all required routes ready to use.

In this case if you run the above command it will list out following routes:

Routes List

Step 6: Install required HTML Package:

I am going to use FORM class in the our blade templates, laravel has removed HTML package from the default configuration, follow next steps to pull html package in using composer

Edit your composer.json file:

Next, use terminal to update composer:

You will see composer is going to pull the package from the repository to our project.

Add new provides in config/app.php file:

Add following classes the aliases array of config/app.php file:

We are good to go the next step to do some designing and coding.

Step 7: Design Layout using Blade Template:

We are going to use layout for all crud pages, Layout is basically a master page for all child pages from the project, Laravel layouts help us to re-use same code within multiple pages.

Create layout page under resources/views/layouts/master.blade.php and add following code:

We have our master page ready to use along with the basic bootstrap design now we will have to pull in bootstrap files from getbootstrap.com and add those files to public directory of the project.

Next you will see @yield('content') – this is section we are going implement in the child pages.

let’s create folder named task under resources/views/, here we are going to store our views so it will good to organise our project folder structure. I would prefer you to use this kind of file structure for every project it’s good to have organised.

List view:

Create resources/views/task/list.blade.php file and following code:

If you have a look on above code, you will see what I was talking about calling our layout page, so at the very top we are extending the layout page and implementing the  content section which is going to render in the content variable of layout.

In content section first, we have bootstrap design I am using bootstrap panel here to get more better look, you can look the bootstrap css classes for panel for example panel-heading and panel-body.

Next there is html table to loop throughout the $tasks variable which are going to see step by step, for now just keep in mind that whenever you need to call, you should pass $tasks variable.

:

Don’t forgot we are going to have pagination at the of the task list, rendor() is going to help to generate pagination.

Let’s have a look on our listing view:

Task List View

Create View:

Create new file under resources/views/task/create.blade.php, we are going to use this view to have our create new task form, use following code and add to the create.blade.php page.

Here again we using same master page, so that’s the reusability Okay next we have our create task form here along with the Error section.

Add New Task View

Edit View:

Add resources/views/task/edit.blade.php and add following code:

Edit View

Show View:

Add resources/views/task/show.blade.php to have task details page, go ahead and use following code:

Show Task View

Step 8: Update TaskController file:

Finally we have to do focus on the import part of the tutorial, which our TaskController, go ahead and edit TaskController.php file, and try to math with the following functions. we needs to update our each function.

 

If you checkout the controller I have added all the operations that we going to needs to handle our resource route along with the blade templates:

Quick description RESTful functions:

index() – use to get task from the table and return with task.list view.

create() – returns task.create view.

store() – handle post request getting from create view, validated the request, create new task and redirect to tasks.index route along with the success message.

show() – find the task details according to the ID and returns task.show view.

edit() – find the task details with the specified ID and returns task.edit view.

update() – handle request coming from edit view, validate the request, update the record and redirect to the tasks.index route.

destroy() – handle Delete method, deletes the record from the table using eloquent modal and redirects back to the tasks.index route.

We are done, I think having read this tutorial you should be able to use laravel now with the it’s important features like eloquent, middleware, routes and controller.

Go start building great laravel applications.

Feel free to comments if you get any issues using this tutorial or you have any question, I always do my best to respond.

Don’t forgot to tell to your friend as well, sharing is caring.

Thanks keep learning….!

  • What does “public $fillable = [ ];” mean?

    • Yogesh Koli

      It is used to set the fillable attributes for the model to serves as a “white list” of attributes that should be mass assignable.