Todo Laravel Project - Routes - Active, Done, Deleted page routes

Laravel Project

In this Laravel project tutorial we will work on the active, done and deleted todo tasks route.

We have already created the home page route in the previous tutorial. Feel free to check that out.

The ACTIVE todo tasks page route

Open the routes/web.php route file and we will get the following route for the ACTIVE todo tasks that we created earlier in the routes tutorial.

Route::get('/todo/active/{page?}', function($page = 1) {
    // code to fetch the todo tasks on page = $page
});

Note! The {$page?} represents an optional parameter.

So, if we visit /todo/active URL then $page parameter in the function will get the default value 1.

And if we visit /todo/active/2 URL then $page parameter will get 2.

We are using this $page variable to fetch the ACTIVE todo tasks of that particular page.

So, let us go ahead and configure our route to fetch only ACTIVE todo tasks for a given page.

Fetch ACTIVE todo tasks for a given page

For this we have to take help of where() and forPage() methods.

Our ACTIVE page route will look like the following.

Route::get('/todo/active/{page?}', function($page = 1) {
    $todo = new Todo();

    $result = $todo
                ->where('status', '=', 'ACTIVE')
                ->forPage($page, 10)
                ->get();

    return $result;
});

Output of the ACTIVE todo tasks page

If we visit 127.0.0.1:8000/todo/active or 127.0.0.1:8000/todo/active/1 we will get max 10 ACTIVE todo tasks from the todos table for page 1 in JSON format.

[
  {
    "id": 1,
    "title": "first work",
    "description": "some work to be done",
    "status": "ACTIVE",
    "created_at": "2018-06-20 09:30:51",
    "updated_at": "2018-06-20 09:30:51"
  },
  {
    "id": 3,
    "title": "3rd work",
    "description": "more work",
    "status": "ACTIVE",
    "created_at": "2018-06-20 20:36:50",
    "updated_at": "2018-06-20 20:36:50"
  },
  ...
  ...
]

To fetch data for page 2 we have to visit 127.0.0.1:8000/todo/active/2.

Note! If there is no data then we will get an empty array [] as a result.

In a similar way we can create DONE and DELETED todo tasks routes.

DONE todo tasks page route

For the DONE todo tasks page we will have the following route code.

Route::get('/todo/done/{page?}', function($page = 1) {
    $todo = new Todo();

    $result = $todo
                ->where('status', '=', 'DONE')
                ->forPage($page, 10)
                ->get();

    return $result;
});

DELETED todo tasks page route

For the DELETED todo tasks page we will have the following route code.

Route::get('/todo/deleted/{page?}', function($page = 1) {
    $todo = new Todo();

    $result = $todo
                ->where('status', '=', 'DELETED')
                ->forPage($page, 10)
                ->get();

    return $result;
});

Okay, we have so far created the home page, active, done and deleted page routes.

We will stop the route work for now and build the view for the home page in the next tutorial.

See you there. Have fun coding :-)