Building a Todo App With Laravel, Vue.js, and Tailwind CSS Part III

by Nick Basileon Jan 4, 2018

Happy New Year! To kick 2018 off right, I think we should finish what we started in 2017. Let's wrap up our Todo app by finishing the rest of the controller actions that we defined last time. To follow along with this tutorial, you can download a copy of this project on GitHub.

Back to the Controller

Hopping back into our project, let's find our Controller at app/Http/Controllers/TodoController.php. If you recall from the last post, we used Laravel to scaffold out our Controller, and took the time to create the index() and store() methods.

Let's take a look at what we have left. We still need to code out the create(), show(), edit(), update(), and destroy() methods. However, since we're using Vue.js and taking a bit more of an API-driven approach, we can ignore the create() and edit() methods because they're used for routing rather than data manipulation. So, let's start by removing create() and edit() from our TodoController.php.

Show()

With those out of the way, we can focus on coding the rest of our methods starting with show(). Out-of-the-box, we can see that Laravel provides us with the following:

public function show(Todo $todo)
{
}

Here we can see that the default method is encouraging us to take advantage of Route Model Binding. This lets us skip some code because we don't have to query for the todo we're looking for. Instead, Laravel recognizes that we're looking for a todo and automatically injects it into our route for us.

Leveraging this handy tool, we can write our method like so:

public function show(Todo $todo)
{
return response($todo, 200);
}

All we're doing here is returning the $todo that Laravel injected for us. Pretty cool, right? With the show() method out of the way, let's jump to the "toughest" method we'll be writing today: update().

Update()

Once again, our scaffold has provided us with a useful method to get started.

public function update(Request $request, Todo $todo)
{
}

This time, we're receiving the $request and then the $todo. So, just like in our store() method, we'll validate the data we're receiving, and then we'll update the todo we received from our Route Model Binding.

And, just like that, our Laravel backend is all done. With our show(), update(), and delete() methods done, we can hop back into our Vue code and wire everything up.

But, before we head over there if you're wondering when we should set up our routes, we took care of that in the last post when we used Route::apiResource('todos', 'TodoController'); in web.php. Once again, Laravel is taking care of us.

Wiring Up Our Vue.js

Up to now, we've been able to get away with just one Vue component. Let's take a look at what we have so far. Inside of resources/assets/js/components we can see our component called todo.vue, which looks like this:

At this point, we have a choice to make. We can keep working with just one component, or we could split it into two components. I think that we should separate them into two components, one for the todo list and another for the todo item itself. This way we can isolate behavior that is specific to the list or the item.

To start, let's rename todo.vue to todo-list.vue. We'll need to update the references to it in app.js and welcome.blade.php too. Now, we can create another component file in the same directory called todo-item.vue.

Before we dive into the code, let's take a second to think about what we want our todo items to do. They'll need to be able to complete and remove themselves like they currently do. Plus, they'll need to be able to update themselves as well.

So, let's have our todo items look and act like they do now, and then we can add an input toggle for editing. So, our todo app will end up looking something like this:

In order to make this happen, we'll need to make some major upgrades to our todo items. They'll need some state management to toggle the editing; methods to handle the updates and deletes, and Tailwind CSS updates to make it all look good.

The Todo Item Template

Putting that all together, our todo-item.vue template will look like this:

Let's walk through this step-by-step. First, we have a wrapper div, which all Vue components need. Inside we have two more divs, one for the read state and another for the edit. We're toggling each of those based on the state.edit data we've set in our component's data. Now, let's take a look at each of those states.

Inside of the first div, we have the read state. This is very similar to what we had before, except with a few tweaks.

The first few are with the checkbox. We can see that it now has a v-model so we can keep track of it when the user interacts with it. We've also bound a method to its click event to update the todo. We'll look at that in detail in a minute.

Moving along to our <p>, we can see that we've updated the Tailwind classes. We've changed it from .w-full to .w-auto so the click target is only the size of the content and not the space.

We've also added some interactive hover classes to our dynamic class. This provides the user with better feedback when they hover the text. Finally, we've bound a startEdit() method to it so we can toggle the editing state.

Our final element, the <button>, is actually the same! So, let's move on to our edit state template.

Here, we've ventured into new territory. We have three elements in this section, an <input> and two <button>s. The <input> is where the user can change the text of the todo.

We've added some Tailwind classes to it that removes the default browser styling, gives it a rounded border, and positions it within its flex context.

Again, we have a v-model that keeps track of the user's inputs. Finally, we've bound the updateTodo method to the enter event so our users don't have to click the button to save their changes.

Our <button>s are pretty much identical. They have the same Tailwind styles, except the first one is teal while the second is red. For those of you who are keeping score at home, this is an excellent chance to extract some component styles.

The <button>s do start to differ when we look at what they do. The first triggers the updateTodo method on click, while the second changes the state with cancelEdit. Additionally, our first <button> is disabled when the data.text is empty to prevent the user from uploading a blank todo.

And that'll wrap up our template! Now, we can take a look at what we've been doing in our <script>.

Let's start at the top again. Right off the bat, we're passing in the todo and index props to our component. This lets us access the data that our todo-list component is managing.

Inside of our data(), we have two objects: state and data. This is a pattern that I've picked up and really like, but we totally could have just set it up like this too.

data(){
return{
edit: false,
text: '',
finished: false,
}
},

The state.edit is taking care of managing when we should be displaying the edit and read sections in the template. While our data is bound to out <input>s, so we can keep track of our user's actions.

Moving along, we can see that in our mounted() lifecycle callback, we're initializing our data() with the todo props details.

Finally, we have our methods() - the bread and butter of this component. Our first method is updateTodo(). Here we're waiting for Vue's next DOM update cycle to emit an event back to todo-list with all the details we need before resetting the edit state.

It's important that we use $nextTick() because we've bound this method to our checkbox. Without it, we'd be firing the event at the same time we're updating the value, so our update wouldn't happen!

Also, this method is using an event bus to emit this information. So, we need to initalize it in our app.js file. Right below window.Vue = require('vue'); we can add window.bus = new Vue();. Now we're cooking with gas.

Our remove() method is doing essentially the same thing as updateTodo - emitting an event back to todo-list with all the details we need to remove a todo.

Meanwhile, startEdit() and cancelEdit are helping us toggle the edit state. startEdit() makes sure we're not editing a completed todo before toggling the state, while cancelEdit() resets the data.text back to its original value.

And that's a wrap! Now, we can take a look at our todo-list's template.

The biggest update is that our v-for is no longer outputting a block of code. Instead, it's outputting our new todo-item component. Additionally, we can see that we're passing it todo and index as props like we defined.

Right off the bat, you can see that we're importing the todo-item so we can use it in the template. Our data() hasn't changed, but created() sure has.

Now, we're calling initListeners() to - you guessed it - initialize the listeners for the emitters we defined in todo-item. Moving along to our methods, we can see that we have initListeners() defined first.

In here, we've registered our listeners with our event bus, and when they're fired they pass their details into the updateTodo() and removeTodo() methods respectively.

In updateTodo() we're making an axios ajax call to our new controller action. Notice that our route uses the todo's id. This is how our Route Model Binding is able to find the todo we need. We're also passing our details.data in the request so we make the changes we defined.

Once the axios call is successful, we splice() in the returned data to our todos at the index of the edited todo in order to maintain our state. Now, it's possible to maintain the state with just Vue, but I've found that this approach minimizes inconsistencies between your backend data and frontend state.

Finally, removeTodo() is doing much the same as updateTodo(). We're making an axios call to our new controller action, and once that's successful we remove the todo from our todos to maintain our state.

Bada bing bada boom, we have ourselves a full-fledged todo app!

The Wrap-Up

There you have it, a todo app built with Laravel, Vue.js, and Tailwind CSS. Looking back on all that we've covered, you should be incredibly proud of yourself. We set up a Laravel project from scratch; added Vue.js and Tailwind CSS; built a Vue todo component and styled it with Tailwind; set up a MySQL database; created a Laravel backend, and finally wired it up to our Vue component. And that doesn't even take into account all the little tweaks and upgrades made along the way.

Even with all of this tremendous progress, there's always more to do. We could make sure it's tested thoroughly with PHP Unit, Vue tests, and Laravel Dusk. We could add user accounts so people can make their own todo lists, and share them with others. We could even spend more time perfecting our UX to make this the best todo app out there!

With your new skills under your belt, I hope that you'll be able to clone the repo and tackle some of these initiatives on your own. But for now, let's call it a day. As always, feel free to ask me any questions on Twitter. And until next time, happy coding!

Nick Basile

I'm a full-stack developer working with Vue.js, Laravel, and more. In my spare time, I read, tweet, blog, and put together a newsletter.