Laravel 5 Tutorial (04) :: Make controller and view for insert and show data (Part 2)

Today we will make a view name with home.blade.php in resources/views folder for make a form to insert data into our database and retrieve data from database. Let’s see
Go to resources/views folder and make a file name with home.blade.php and write code below

You can read previous posts

    <div class="container">
        <div class="row">
            <div class="col-md-10">
                <form action="" method="POST" class="form-inline">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <p>Work name</p>
                        <input type="text" class="form-control" name="workName"/>
                        <span><input type="submit" class="btn btn-success" value="Save"/></span>
                <table class="table">

This view looks like below when you browse http://localhost/todo/public/ 



Open the routes.php file and add another route for insert data from our view(home.blade.php)


This route means our form will send post request in same path. Store method in TodoController will handle the request.

Add these at the very first of TodoController file

use Input;
use App\Todo;

Now open the TodoController and go to  store method. Define the store method like as below

public function store()
            return redirect()->back();

Our form creation and handle the form post request for insert data has been finished. Now we will have to make a model.

open the command window and type php artisan make:mode Todo and press enter.

Now open the Todo model from App folder and define the model like below

<?php namespace App;
use Illuminate\Database\Eloquent\Model;

class Todo extends Model {
    protected $table = 'todo';
    protected $fillable = ['work','status', 'created_at'];

Okay our data insertion procedure has finished. Now try to insert data by form. If you have done all things successfully then you can insert data successfully. After insert data please check in your database data is inserted or not. In next tutorial we will retrieve data from our database.

Share on :

2 Responses to “Laravel 5 Tutorial (04) :: Make controller and view for insert and show data (Part 2)”

  1. sani says:

    hi, i have followed all steps for todo project from first to last but when i submit the data for insert then it gives the error

    Whoops, looks like something went wrong.

    • Harun says:

      Thanks for your comment,please change debug mode true in config folder app.php then set debug=>true. Now you can see what wrong with you.

Leave a Reply

× five = 25