# Laravel Tutorial: Todo controller

Generate Todo controller

Generating a controller is easy. Just run the following command:

$ php artisan make:controller TodoController

Once the controller is created, open it in your favorite editor. The location for the newly created controller is found in: laravel-todolist/app/Http/Controllers/TodoController.php

Replace the generated code with this (changes are highlighted):


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Todo;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class TodoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return Todo::all();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $todo = new Todo;

        if ($todo->validate($request->all())) {
            $todo->todo = $request->todo;
            $todo->completed = $request->completed;

            $todo->save();

            return $todo;
        } else {
            $errors = $todo->errors();
            return ["errors" => $errors];
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return Todo::find($id);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $todo = Todo::find($id);

        $todo->todo = !empty ($request->todo) ? $request->todo : $todo->todo;
        $todo->completed = isset ($request->completed) ? $request->completed : $todo->completed;

        $todo->save();

        return $todo;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $todo = Todo::find($id);
        $todo->delete();
        return $todo;
    }
}

Let's breakdown the above code:

  • The index() instance method returns a list of Todo items from the table.
  • The store($request) instance method creates a new Todo item if and only if the data received validates. On success the newly created Todo item is returned. Else an error is returned.
  • The show($id) instance method returns a single Todo item with an id of $id.
  • The update($request, $id) instance method updates the existing Todo item when the data changes.
  • The destroy($id) instance method deletes the Todo item from the table.