Todo Laravel Project - Routes - Home page route

Laravel Project

In this Laravel project tutorial we will work on the home page route.

The home page route

In the routes tutorial we created all the routes for our project inside the routes/web.php file.

At the moment our home page route looks like the following.

Route::get('/', function() {
    // code to fetch todo tasks
});

Fetching data from the todos table

To fetch the rows from the todos table we have to take help of the Todo model that we created in the Model tutorial.

At the beginning of the routes/web.php file add the following line.

use App\Todo;

Now inside the home route write the following line to fetch all the rows in the todos table.

Route::get('/', function() {
    $todo = new Todo();

    $result = $todo->all();

    return $result;
});

So, inside this route we are creating an object of the Todo model class.

Then, we are calling the all() method provided by the model to fetch all the rows in the todos table.

Finally we are returning the result which will be displayed when we visit the home page.

Checking the output

Open terminal and run the following command $ php artisan serve and then visit the link were the project is being served. By default, it is 127.0.0.1:8000.

If everything went well then we will get to see the following output.

[
  {
    "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": 2,
    "title": "second work",
    "description": "some more work",
    "status": "DONE",
    "created_at": "2018-06-20 09:35:46",
    "updated_at": "2018-06-20 10:05:24"
  },
  ...
  ...
]

Note! Laravel is smart enough to convert the result and display it in JSON format.

We will create views for the web pages in the views tutorials.

Alright, we are able to fetch all the rows but we want to show only the ACTIVE todo tasks in the home page.

So, lets go ahead and add some conditions.

Fetching only ACTIVE todo tasks

To fetch only the ACTIVE todo tasks we have to take help of where clause. So, our code will now look like the following.

Route::get('/', function() {
    $todo = new Todo();

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

    return $result;
});

If we now visit the home page we will get only the ACTIVE todo tasks 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"
  },
  ...
  ...
]

Setting per page limit

Now we will limit 10 records per page. For that we will take help of forPage() which takes two arguments. The first argument is the page and the second argument is the total number of items.

In our case, we want to fetch page 1 and total 10 items for the page. So, we will use forPage(1, 10).

Our route will now look like the following.

Route::get('/', function() {
    $todo = new Todo();

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

    return $result;
});

Now, if we open the home page we will get max 10 ACTIVE todo tasks in JSON format.

In the next tutorial we will work on the ACTIVE, DONE and DELETED route.

Have fun coding :)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT