February 2, 2015 11:53 am

Building A To Do List with Ajax Using Laravel 4 – Part 1

In this new tutorial series we will be using Laravel and jQuery to build a to-do list with Ajax. Through this application building we will learn how to use RESTFul controllers, RESTFul routing and Request types. We will be dividing this application development into different segments.

Building A To Do List with Ajax Using Laravel 4 - Part I

  • Creating and migrating our to do list’s database.
  • Creating a to do list model
  • Creating the template
  • Inserting data to the database with Ajax
  • Retrieving the list from the database
  • How to allow only Ajax requests

Let’s create a new project using below Artisan CLI command and Composer.

Let’s verify laravel installation by navigating to the Laravel project folder using browser. If you see laravel logo then it means laravel project creation successful.

laravel home page

Once laravel project creation successful we have to setup database configuration on app/config/database.php file against the database driver which we like to use. In this case we are going to use MySQL so our database configuration will look like following.

Creating and migrating our to do list’s database

Please check to previous tutorials to know more about migrations, how they are helpful to control development steps. To create our to do list migration, we have to use following Artisan CLI command:

When we run this command, Artisan will generate a migration to generate a database table named tasks. Now we need edit this migration file for the necessary database table columns. We can find the migration file at app/database directory.

To build a simple to do list, we need five columns:

  • The id column will store ID numbers of to do tasks
  • The title column will store a to do task title
  • The status column will store the status of each to do task
  • The created_at and updated_at columns will store the created and updated dates  of the to do tasks

If we use $table->timestamps() in the migration file, Laravel migration class automatically creates created_at  and updated_at columns. To apply migrations we have to use following command:

After running this command, we can see tasks table and columns have been created in our database. Now we have to write a model for this migration.

Creating a tasks model

To create a model, we should open a new file and save it in app/models/ directory with name task.php. We have to write the following code in this new model.

As we see our model, our Task class extends an Eloquent model, which is the ORM database class of Laravel. The protected $table = ‘tasks’; code tells Eloquent about our model’s table name. If we don’t set this table variable, Eloquent accepts the plural version of the lower case model name as the table name. Now our application needs a template file.

Creating the Template

Laravel uses a template engine called Blade for static and application template files. Laravel calls the template files from the app/views directory, so we need to create our first template under this directory and also named it as index.blade.php.

Our application will have below features based on that we will define our template code.

  • Adding a new Task
  • Updating the existing Task
  • Deleting the existing task
  • Updating the task status
  • Listing tasks

First we will see how we can add a task using Ajax. For adding a new task we need a form to handle task title as input. So let’s create a form.

In the above code we defined a form with id add_task and having an input variable called title to take care task title by user. At present we will be hiding this form because we will show this form when user wants to add a task only.

Once we add task using the above form we have to list them in a table so let’s create a table.

In the above code we are giving a link to add task. When user clicks on add task link then we have to show the form. For this we are calling a function called show_form(‘add_task’); onclick of the link. So let’s write this javascript function in a new file in public/assets/js  directory and we will name this as script.js file.

In the above function onclick of the link we are hiding all forms using javascript hide() method and then we are using form_id to show the add task form.

Let’s assume at present we get all the tasks from database in an array from controller called tasks, To list tasks we have to loop through the tasks array. While looping we are separating each task based on the task status. If the task status set to 1 then it is completed otherwise it’s incomplete. So we are providing task status update link to update task completion.

We are also providing options to edit and delete tasks. Let’s see those functions below.

Once user decided to add a task, he will see a add task form but what will happen when he submits with user input. At present we are not taking care of the add task and edit task functionalities by the user. So as per our application aims, we have to do these actions using Ajax. So let’s see how we can do these actions.

we will continue the next part of this tutorial in next post. In this tutorial we have built migrations, database schema design, created model to take care database queries and created the template also.


Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories: