Todo Laravel Project - Handle form submission - Create new todo task

Laravel Project

In this Laravel project tutorial we will learn to handle form submission and create a new todo task.

In the previous tutorial Form to create new todo tasks we learned how to create a form that will help to create a new todo tasks. Feel free to check that out.


Include the following line at the top of the resources/web.php file.

use Illuminate\Http\Request;

The new todo tasks route

Inside the routes/web.php file we will get the following code for the new todo tasks route.

Route::post('/todo', function() {
    // code to create new todo task

Modify the route to the following.

Route::post('/todo', function(Request $request) {

    // validate
    $validator = Validator::make($request->all(), [
        'todo-title' => 'required|max:100',
        'todo-description' => 'required|max:5000',

    // if error
    if ($validator->fails()) {
        return 'Error in submitted data.';

    // now create new todo
    $todo = new Todo();

    if (isset($request['todo-title'])) {
        $todo->title = $request['todo-title'];
    if (isset($request['todo-description'])) {
        $todo->description = $request['todo-description'];

    // now save

    // redirect to home
    return redirect('/');


In the above code the todo-title field is required and max number of characters allowed is 100.

Similarly, the todo-description field is required and max number of characters allowed is 5000.

If there is no error then we are creating an object of the Todo model.

Then we are setting the title and description of the $todo object.

Next we are calling the save() method to insert the data into the todos table.

If everything goes well then on submitting the form we will have a new entry in the ACTIVE page.

Note! The new ACTIVE todo task will be added at the very end page in the ACTIVE page.

Since, we are redirecting to the home page on form submit so, we can list the ACTIVE todo tasks in the home page in latest-first order.

Order by DESC

Head over to home page route and make the following changes.

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

    $result = $todo
                ->where('status', '=', 'ACTIVE')
                ->orderBy('created_at', 'DESC')
                ->forPage(1, 10)

    // return $result;
    return view('home', ['todos' => $result]);

We have added orderBy('created_at', 'DESC') and this will list the rows in descending order based on created_at column. So, newly added ACTIVE tasks will be placed at the top.

If we now create a new todo task and check the home page we will see the new task at the top.

In the next tutorial we will learn to display todo tasks detail.

See you there. Have fun :)