Form Example in Laravel 8

Sanajit Jana
4 min readSep 12, 2021
Form Example in Laravel 8

Laravel 8 form example tutorial. In this post, I will teach from starting on how to send form data on the controller and how to insert form data in a database using laravel 8.

If you are trying to create a form and want to insert form data into a database using laravel 8 latest versions. So this post will help you to do this.

Because in this post example, I will create a contact-list form and submit it to the database using laravel 8 versions.

How to Submit Form Data into Database?

  • Step 1 — Install Laravel 8 Application
  • Step 2 — Configuring Database using Env File
  • Step 3 — Create Model & Migration File For Add Blog Post Form
  • Step 4 — Create Routes
  • Step 5 — Creating Controller
  • Step 6 — Create Blade File For Add Blog Post Form
  • Step 7 — Start Development Server
  • Step 8 — Run Laravel 8 Form App On Browser

Step 1 — Install Laravel 8 Application

In step 1, open your terminal and navigate to your local web server directory using the following command:

//for windows user
cd xampp/htdocs
//for ubuntu user
cd var/www/html

Then install laravel 8 latest application using the following command:

composer create-project --prefer-dist laravel/laravel LaravelForm

Step 2 — Configuring Database using Env File

In step 2, open your downloaded laravel 8 apps into any text editor. Then find the .env file and configure database detail like the following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Step 3 — Create Model & Migration File For Add Blog Post Form

In step 3, open a command prompt and navigate to your project by using the following command:

cd / LaravelForm

Then create a model and migration file by using the following command:

php artisan make:model Post -m

The above command will create two files into your laravel 8 form application, which is located inside the following locations:

  • LaravelForm/app/Models/Post.php
  • LaravelForm/database/migrations/2020_09_09_025857_create_posts_table.php

So, find create_posts_table.php file inside LaravelForm/database/migrations/ directory. Then open this file and add the following code into function up() on this file:

public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description');
$table->timestamps();
});
}

Now, open again your terminal and type the following command on cmd to create tables into your selected database:

php artisan migrate

Step 4 — Create Routes

In step 4, open your web.php file, which is located inside the routes directory. Then add the following routes into the web.php file:

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('add-blog-post-form', [PostController::class, 'index']);
Route::post('store-form', [PostController::class, 'store']);

Step 5 — Creating Controller

In step 5, create a form controller by using the following command:

php artisan make:controller PostController

The above command will create a PostController.php file, which is located inside LaravelForm/app/Http/Controllers/ directory.

So open the PostController.php file and add the following code into it:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
return view('add-blog-post-form');
}
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->description = $request->description;
$post->save();
return redirect('add-blog-post-form')->with('status', 'Blog Post Form Data Has Been inserted');
}
}

Step 6 — Create Blade File For Form

In step 6, create a new blade view file named add-blog-post-form.blade.php inside resources/views directory for add blog post form.

Then add the following HTML form code to add-blog-post-form.blade.php:

<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Form Example Tutorial</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
@if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<div class="card">
<div class="card-header text-center font-weight-bold">
Laravel 8 - Add Blog Post Form Example
</div>
<div class="card-body">
<form name="add-blog-post-form" id="add-blog-post-form" method="post" action="{{url('store-form')}}">
@csrf
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" id="title" name="title" class="form-control" required="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Description</label>
<textarea name="description" class="form-control" required=""></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>

Step 7 — Start Development Server

Finally, open your command prompt again and run the following command to start the development server for your laravel 8 form application:

php artisan serve

Step 8 — Run Laravel 8 Form App On Browser

In step 8, open your browser and fire the following URL into your browser:

http://127.0.0.1:8000/add-blog-post-form

When you fire the above-given URL on the browser, you will look like in the following image:

--

--

Sanajit Jana

As a Web Designer and Developer, I design, build digital products, specializing in custom web theme development and I love what I do.