🎉 New release for Pusher Chatkit - Webhooks! Extend your in-app chat functionality
Hide

Build a to-do list app with Vue.js and Laravel

  • Ethiel Adiassa
November 27th, 2018
You will need Laravel 5.7 installed on your machine.

Introduction

In this tutorial, we will build a to-do app with Vue.js and Laravel using Pusher to add the realtime functionality. Vue.js is such a great JS framework as it speeds up frontend development and helps achieve more with less code. Laravel on its side is one the most popular and powerful backend frameworks; allied with Vue.js we can build awesome web apps. Pusher you may know is a collection of hosted APIs to build realtime apps with less code. Now let’s get started!

Demo

This is the result of our final working app:

Prerequisites

In order to follow this tutorial a basic or good understanding of Vue.js and Laravel is required, as we’ll be using these technologies throughout this tutorial. Also ensure you have npm or Yarn on your machine.

We’ll be using these tools to build our application:

Pusher setup

Head over to the Pusher website and sign up for a free account. Select Create new app on the sidebar, and hit Create my app to create a new app after filling the form.

Once your app is created, retrieve your credentials from the API Keys tab, and make note of them as we’ll use them later in the tutorial.

Initialize the project and install dependencies

To get started we’ll install a new Laravel application using the Laravel CLI. We’ll run the following command:

laravel new realtime_todo

Once the installation is finished run the following command to move to your app directory:

cd realtime_todo

Now we’ll install our node dependencies, first paste this in your package.json file:

    {
      "private": true,
      "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
      },
      "devDependencies": {
        "axios": "^0.18",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^2.0",
        "lodash": "^4.17.5",
        "popper.js": "^1.12",
        "vue": "^2.5.7",
        "vuex": "^3.0.1",
        "laravel-echo": "^1.4.0",
        "pusher-js": "^4.2.2"
      }
    }

Then run npm install or yarn to install the dependencies. It’s up to you.

After this step, add the following to your .env file in the root of your project directory. Ensure to replace the placeholders with your keys from Pusher.

    PUSHER_APP_ID=YOUR_PUSHER_APP_ID
    PUSHER_APP_KEY=YOUR_PUSHER_APP_KEY
    PUSHER_APP_SECRET=YOUR_PUSHER_APP_SECRET
    PUSHER_APP_CLUSTER=YOUR_PUSHER_APP_CLUSTER

Database setup

In this tutorial, we’ll use SQLite as our database. Create a database.sqlite file in the database directory, and amend the .env file like this:

    DB_CONNECTION=sqlite
    DB_DATABASE=/absolute/path/to/database.sqlite

Refer to this section on Laravel website for more relevant information.

Building models and seeding our database

Now, let’s build our database structure. We’ll use the Laravel CLI again for that. Run this command:

php artisan make:model Task -mc

The above command will generate the Task model as well as its migration and its controller TaskController.php for us.

Open your Task.php file and paste this:

    //realtime_todo/app/Task.php

    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Task extends Model
    {
        //
        protected $fillable = ['title','completed'];
    }

Next copy and paste this piece of code in your task migration file:

    //realtime_todo/database/migrations/*_create_tasks_table.php

    <?php
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    class CreateTasksTable extends Migration
    {
        /**
        * Run the migrations.
        *
        * @return void
        */

        public function up() {
            Schema::create('tasks', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title');
                $table->boolean('completed')->default(false);
                $table->timestamps();
            }
            );
        }

        /**
        * Reverse the migrations.
        *
        * @return void
        */
          public function down(){
            Schema::dropIfExists('tasks');
        }
    }

Then run php artisan migrate to run the migration.

Define routes and create the TaskController

In this section, we’ll define our app endpoints and define the logic behind our TaskController.php

This is a simple CRUD(create, read, update, delete) over our Task model. So we defined our routes with corresponding functions to handle our browser requests. Paste the following into api.php:

    //realtime_todo/routes/api.php
    <?php
    use Illuminate\Http\Request;

    /*
    --------------------------------------------------------------------------
    | API Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register API routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | is assigned the "api" middleware group. Enjoy building your API!
    |
    */

    Route::get('todos','TaskController@fetchAll');
    Route::post('todos','TaskController@store');
    Route::delete('todos/{id}','TaskController@delete');

Now let’s define our controller logic. Our controller functions will be responsible for actions to handle when some requests reach our API endpoints.

Open your TaskController file and paste the following code:

    //realtime_todo/app/Http/Controllers/TaskController.php

    <?php

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Events\TaskCreated;
    use App\Events\TaskRemoved;
    use App\Task;
    class TaskController extends Controller
    {
        //
        public function fetchAll(){
            $tasks = Task::all();
            //return response()->json($tasks);
            return $tasks;
        }

        public function store(Request $request){
            $task = Task::create($request->all());
            broadcast(new TaskCreated($task));
            return response()->json("added");
        }

        public function delete($id){
            $task = Task::find($id);
            broadcast(new TaskRemoved($task));
            Task::destroy($id);
            return response()->json("deleted");
        }
    }

In the above code we have three functions fetchAll, store , and delete:

  • fetchAll: queries our database to return all our tasks/to-dos
  • store: creates a new to-do with request params(title and task status)
  • delete: finds a task and deletes from the database.

Emit events

Well you may have noticed these lines: broadcast(new Taskcreated($task)) and broadcast(new TaskRemoved($task)) respectively in store and delete functions. What is their purpose? Through these lines we emit events with a task model instance.

You can get more relevant information about Laravel broadcasting here. In the next part of the tutorial, we’ll see how to create these events..

Create our events

In this part we’ll create events we talked about above: TaskCreated and TaskRemoved events.

TaskCreated event

Our TaskCreated event will be emitted whenever a new to-do or task is created. Enough talk, let’s focus on the code. Let’s create our TaskCreated by running the following command in your terminal: php artisan make:event TaskCreated.

Now open your TaskCreated file and paste the following:

    //realtime_todo/app/Events/TaskCreated.php
    <?php

    namespace App\Events;
    use Illuminate\Broadcasting\Channel;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Broadcasting\PrivateChannel;
    use Illuminate\Broadcasting\PresenceChannel;
    use Illuminate\Foundation\Events\Dispatchable;
    use Illuminate\Broadcasting\InteractsWithSockets;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;


    class TaskCreated implements ShouldBroadcastNow
    {
        use Dispatchable, InteractsWithSockets, SerializesModels;

        /**
        * Create a new event instance.
        *
        * @param $task
        * @return void
        */

        public $task;
        public function __construct($task)
        {
            //
            $this->task = $task;
        }

        /**
        * Get the channels the event should broadcast on.
        *
        * @return \Illuminate\Broadcasting\Channel|array
        */

        public function broadcastOn()
        {
            return new Channel('newTask');
        }

        public function broadcastAs(){
            return 'task-created';
        }

    }

Our class constructor initializes a task that is created. We have two additional functions that may seem strange to you:

  • broadcastAs: customizes the broadcast name because by default Laravel uses the event’s class name.
  • broadcastOn: defines the channel task-created (which we’ll set up further on the tutorial) on which our event should be broadcast.

TaskRemoved event

This event is broadcast when a task is deleted and we want to notify other users of that.

As you may guess, you can run: php artisan make:event TaskRemoved to create the event. Now head up to your TaskRemoved file:

    //realtime_todo/app/Events/TaskRemoved.php

    <?php
    namespace App\Events;
    use Illuminate\Broadcasting\Channel;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Broadcasting\PrivateChannel;
    use Illuminate\Broadcasting\PresenceChannel;
    use Illuminate\Foundation\Events\Dispatchable;
    use Illuminate\Broadcasting\InteractsWithSockets;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
    class TaskRemoved implements ShouldBroadcastNow
    {
        use Dispatchable, InteractsWithSockets, SerializesModels;

        /**
        * Create a new event instance.
        *
        *  @param $task
        * @return void
        *
        */
            public $task;
            public function __construct($task)
            {
            //
            $this->task = $task;
        }

        /**
        * Get the channels the event should broadcast on.
        *
        * @return \Illuminate\Broadcasting\Channel|array
        */

        public function broadcastOn()
        {
            return new Channel('taskRemoved');
        }

        public function broadcastAs(){
            return 'task-removed';
        }

    }

This class structure is pretty similar to the previous one, so we won't spend further time explaining its functions.

Don’t forget to implement ShouldBroadcastNow to enable Pusher broadcasting events as soon as they occur.

Broadcast configuration

According to Laravel documentation about event broadcasting, before broadcasting any events, you will first need to register the App\Providers\BroadcastServiceProvider. In fresh Laravel applications, you only need to uncomment this provider in the providers array of your ../config/app.php configuration file. This provider will allow you to register the broadcast authorization routes and callbacks.

You may also set the encrypted value to false in order to avoid a 500 error while broadcasting events with Pusher.

If this is done, you have to tell Laravel to use Pusher to broadcast events. Open your .env file and ensure you have this line: BROADCAST_DRIVER=pusher

As we are broadcasting our events over Pusher, we should install the Pusher PHP SDK using the Composer package manager:

    composer require pusher/pusher-php-server "~3.0"

Setting up the broadcast channel

Laravel broadcasts events on well defined channels. As said above our events should be broadcast on particular channels. It’s time to set them up. Paste the following code in your channels.php file:

    //realtime_todo/routes/channels.php
    <?php

    /*
    ------------------------------------------------------------------------
    | Broadcast Channels
    |--------------------------------------------------------------------------
    |
    | Here you may register all of the event broadcasting channels that your
    | application supports. The given channel authorization callbacks are
    | used to check if an authenticated user can listen to the channel.
    |
    */
    Broadcast::channel('newTask', function(){
        return true;
    });
    Broadcast::channel('taskRemoved', function(){
        return true;
    });

As we aren’t using Laravel auth, we return true in the function callback so that everybody can use this channel to broadcast events.

Set up Laravel Echo

We’ll use Laravel Echo to consume our events on the client-side.

Open your resources/js/bootstrap.js file and uncomment this section of the code:

    import Echo from 'laravel-echo'

    window.Pusher = require('pusher-js');

    window.Echo = new Echo({
        broadcaster: 'pusher',
        key: process.env.MIX_PUSHER_APP_KEY,
        cluster: process.env.MIX_PUSHER_APP_CLUSTER,
        encrypted: false
    });

While uncommenting, you may also set the encrypted property to false to avoid a 500 error while trying to broadcast events with Pusher channels.

The above code sets up Laravel Echo with Pusher. This will make our app aware of events broadcast, and Laravel Echo will consume our events

Our app is ready to broadcast and consume events in realtime using Pusher channels. Let’s focus now on the frontend of your app.

Set up Vuex store

We’ll be using the Vuex library to centralize our data and control the way it is mutated throughout our application.

Create our state

Vuex state is a single object that contains all our application data. So let’s create ../resources/js/store/state.js and paste this code inside:

    let state = {
        todos: [],
        toRemove: null,
        newTodo: {
            title: '',
            completed: false
        }
    }
    export default state

Our state objects holds :

  • todos: holds our to-dos got from the backend
  • toRemove: holds temporarily the to-do we intend to remove
  • newTodo: holds details about a new to-do we are about to add

Create our getters

With help of getters we can compute derived state based on our data store state. Create ../resources/js/store/getters.js and paste this code inside

    let getters = {
        newTodo: state => {
            return state.newTodo
        },
        todos: state => {
            return state.todos
        },
        toRemove: state => {
            return state.toRemove
        }
    }
    export default getters

Create our mutations

Mutations allow us to perform some changes on our data. Create ../resources/js/store/mutations.js and paste this piece of code inside:

    let mutations = {
        ADD_TODO(state, todo) {
            state.todos.unshift(todo)
        },
        CACHE_REMOVED(state, todo) {
          state.toRemove = todo;
        },
        GET_TODOS(state, todos) {
            state.todos = todos
        },
        DELETE_TODO(state, todo) {
            state.todos.splice(state.todos.indexOf(todo), 1)
            state.toRemove = null;
        }
    }
    export default mutations

Here we have three mutations:

  • ADD_TODO: adds a new to-do to the top our to-dos list
  • CACHE_REMOVED: keeps track temporarily of the to-do to remove
  • GET_TODOS: sets our to-dos list given some data
  • DELETE_TODO: responsible for deleting a to-do from our to-dos list

Create our actions

Vuex actions allow us to perform asynchronous operations over our data. Create the file ../resources/js/store/actions.js and paste the following code:

    let actions = {
      ADD_TODO({commit}, todo) {
            axios.post('/api/todos', todo).then(res => {
                if (res.data === "added")
                    console.log('ok')
            }).catch(err => {
                console.log(err)
            })
        },
        DELETE_TODO({commit}, todo) {
            axios.delete(`/api/todos/${todo.id}`)
                .then(res => {
                    if (res.data === 'deleted')
                        console.log('deleted')
                }).catch(err => {
                    console.log(err)
                })
        },
        GET_TODOS({commit}) {
            axios.get('/api/todos')
                .then(res => {
                    {  console.log(res.data)
                        commit('GET_TODOS', res.data)
                    }
                }).catch(err => {
                    console.log(err)
                })
        }
    }
    export default actions

We have defined two actions and each of them responsible of a single operation. They perform asynchronous calls to our API routes.

  • ADD_TODO makes a POST request to our api/todos endpoint to add a new task. This action is dispatched whenever the user is submitting a task to add.
  • GET_TODOS sends a GET request to our api/todos endpoint to get our database to-dos/tasks and commits the response with GET_TODOS mutation.
  • DELETE_TODO performs a DELETE a request to our api/todos/{id} endpoint in order to remove a to-do from our to-dos list.

Set up our store with Vue

Create the file ../resources/js/store/index.js and paste this code inside:

    import Vue from 'vue'
    import Vuex from 'vuex'
    import actions from './actions'
    import mutations from './mutations'
    import getters from './getters'
    import state from "./state";

    Vue.use(Vuex);

    export default new Vuex.Store({
        state,
        mutations,
        getters,
        actions
    })

Then, we export our store and add it to the Vue instance. Replace the existing code in your ../resouces/js/app.js file with the following:

    require('./bootstrap');
    window.Vue = require('vue');

    Vue.component('todo', require('./components/Todo'))
    Vue.component('new-todo', require('./components/NewTodo.vue'))
    Vue.component('todo-list', require('./components/TodoList'))
    Vue.component('todo-app', require('./components/TodoApp'))
    import store from '../js/store'

    const app = new Vue({
        el: '#app',
        store
    });

The previous code also globally registers four Vue components, Todo.vue ,NewTodo.vue,TodoList.vue and TodoApp.vue that we’ll build in the next part of this tutorial.

Building Vue components

We’ll build four Vue components for our app as we said above, so let’s do it.

Create the Todo.vue component

The Todo.vue component is responsible for encapsulating details about a single Task instance from the database and rendering it in a proper and styled way. Create a Todo.vue file in your ../resources/js/components directory and paste the following inside:

    //../resources/js/components/Todo.vue
    <template>
     <li class="todo" :class="{ completed: todo.completed }">
         <div class="view">
             <input type="checkbox" v-model="todo.completed" class="toggle">
             <label>{{todo.title}}</label>
             <button @click="removeTodo(todo)" class="destroy"></button>
         </div>
     </li>
    </template>
    <script>
    export default {
      name: "Todo",
      props: ["todo"],

      methods: {
        removeTodo(todo) {
          this.$store.commit("CACHE_REMOVED", todo)
          this.$store.dispatch("DELETE_TODO", todo);
        }
      }
    };
    </script>

Our Todo.vue component takes a todo property whose details we render in the component body using the HTML <li></li> tag. The component has the removeTodo function that takes the to-do we intend to remove as argument, caches it temporarily (via the CACHE_REMOVED mutation) and dispatches the DELETE_TODO action to remove it.

Create the NewTodo.vue component

We’ll use this component to add a new task to our to-dos list. This component should be very simple to code. I promise you 😉. First create the NewTodo.vue file inside the same directory as above and paste this inside:

    //../resources/js/components/NewTodo.vue
    <template>
         <input type="text" v-model="newTodo.title"
         @keyup.enter="addTodo" autofocus="autofocus"
          placeholder="What are you trying to get done?" class="new-todo">
    </template>
    <script>
    import { mapGetters } from "vuex";
    export default {
      name: "NewTodo",
      methods: {
        addTodo() {
          this.$store.dispatch("ADD_TODO", this.newTodo);
        }
      },
      computed: {
        ...mapGetters(["newTodo"])
      }
    };
    </script>

This component is composed of a simple input field to enter our to-do title. We append a @keyup.enter event so we can execute the addTodo function whenever the user hits the Enter key of his keyboard. Nice play isn’t it 😎 ?! We get the newTodo state object from our getters using Vue.js mapGetters helper and bind it to our input. As I said above the newTodo object should contain information of a new todo we want to add to our to-dos list. The addTodo function dispatches the ADD_TODO action having our newTodo as a parameter.

Create the TodoList.vue component

This component will render to-dos items from database. It’s that simple. So create your TodoList.vue component and paste this code inside:

    //../resources/js/components/TodoList.vue
    <template>
       <ul class="todo-list">
         <todo v-for="todo in todos" :todo="todo" :key="todo.id" />
       </ul>
    </template>

    <script>
    import { mapGetters } from "vuex";
    import todo from "../components/Todo";
    export default {
      components: {
        todo
      },
      name: "TodoList",
      mounted() {
        this.$store.dispatch("GET_TODOS");
      },
      computed: {
        ...mapGetters(["todos"]),
      }
    };
    </script>

In the mounted hook function we dispatch the GET_TODOS action to get our to-dos list item, and we use Vuex helper function …mapGetters() to access our todos state. We loop over our to-dos list and render a todo component (imported from Todo.vue component) which takes the current loop item as a property.

Create the TodoApp.vue component

In this component we simply merge the first three components we created and listen to Pusher realtime events. Create your TodoApp.vue component and paste the following inside:

    //../resources/js/components/TodoApp.vue

     <template>
         <section class="todoapp">
                <header class="header">
                  <h1>todos</h1>
                </header>
                <new-todo></new-todo>
                <todo-list></todo-list>
         </section>
     </template>
     <script>
    import newTodo from "../components/NewTodo.vue";
    import todoList from "../components/TodoList.vue";
    import { mapGetters } from "vuex";

    export default {
      components: {
       newTodo,
       todoList
      },
      name: "TodoApp",
      mounted() {
        window.Echo.channel("newTask").listen(".task-created", e => {
          this.$store.commit("ADD_TODO", e.task);
          this.newTodo.title = "";
        });
        window.Echo.channel("taskRemoved").listen(".task-removed", e => {
            this.$store.commit("DELETE_TODO", this.toRemove);
        });
      },
      computed: {
        ...mapGetters(["newTodo", "toRemove"])
      }
    };
    </script>

In the mounted function of our component, we are subscribing to two channels:

  • newTask channel: we listen the task-created event triggered when a new to-do is added to the list. Then we commit the ADD_TODO mutation with the task sent carried by the event, in order to add it to our to-dos list. Finally we reset our newTodo we import from our Vuex store.
  • taskRemoved channel, this channel enables to listen to the task-removed event triggered when a task/to-do is removed from our list. When the event is emit, we assign the task deleted to our toRemove object we set up in our Vuex store, and we commit the DELETE_TODO mutation to finally remove it from to-dos list.

Finalize the app

Now, let’s replace of our welcome.blade.php with the following containing our TodoApp component:

    //realtime_todo/resources/views/welcome.blade.php

    <!doctype html>
    <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="csrf-token" content="{{ csrf_token() }}" />
            <title>Realtime to-do app</title>
            <!-- Fonts -->
            <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">
            {{--  Style  --}}
            <link href="{{ mix('css/app.css') }}" rel="stylesheet" type="text/css">
            <!-- Styles -->
            <style>
                html, body {
                    background-color: #fff;
                    color: #636b6f;
                    font-family: 'Nunito', sans-serif;
                    font-weight: 200;
                    height: 100vh;
                    margin: 0;
                }
                .full-height {
                    height: 100vh;
                }
                .flex-center {
                    align-items: center;
                    display: flex;
                    justify-content: center;
                }
                .position-ref {
                    position: relative;
                }
                .top-right {
                    position: absolute;
                    right: 10px;
                    top: 18px;
                }
                .content {
                    text-align: center;
                }
                .title {
                    font-size: 84px;
                }
                .links > a {
                    color: #636b6f;
                    padding: 0 25px;
                    font-size: 12px;
                    font-weight: 600;
                    letter-spacing: .1rem;
                    text-decoration: none;
                    text-transform: uppercase;
                }
                .m-b-md {
                    margin-bottom: 30px;
                }
            </style>
        </head>
        <body>
              <div id="app">
                        <todo-app></todo-app>
              </div>

              <script src="{{mix('js/app.js')}}"></script>
        </body>
    </html>

To style our app, get this file and replace the content inside your ../resources/sass/app.scss file with it.

Now open your terminal and run npm run dev to build your app in a proper way. This can take a few seconds. After this step run php artisan serve and open your browser at localhost:8000 to see your app working fine. You can try to add a new to-do to your list and see things working in realtime if you carefully followed steps above. You are now a boss 😎

Note: If you encounter a 500 error when trying to add or delete to-dos, it’s sure that you have to disable Pusher encryption as I suggested you. Open these files ../config/broadcasting.php and ../resources/js/bootstrap.js and make sure you disable Pusher encryption encrypted: false in both of them.

Conclusion

In this tutorial we’ve created a realtime to-do app using Laravel, Vue.js, and Pusher to provide realtime functionality. You can think up new ideas to extend the application. It’ll be fun to see what you come up with. The source code for this tutorial is available on GitHub here.

Clone the project repository
  • JavaScript
  • Laravel
  • PHP
  • Vue.js
  • Channels

Products

  • Channels
  • Chatkit
  • Beams

© 2019 Pusher Ltd. All rights reserved.

Pusher Limited is a company registered in England and Wales (No. 07489873) whose registered office is at 160 Old Street, London, EC1V 9BW.