Laravel 8 Form Example Tutorial

In this tutorial i’m going to describe to create form and store data in database. Please follow some easy steps mentioned below.

How to Submit Form Data into Database in Laravel 8

  • 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

composer create-project laravel/laravel laravelform
  • Step 2 — Configuring Database using Env File

Now create model and migration table

php artisan make:model Post -m

Next go to post migration table and paste below code

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('description');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}

Next go to Post model and paste below code

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
protected $fillable=[
'title',
'description'
];
}

Now migrate the table run below command

php artisan migrate

Step 4 — Create Routes

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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

php artisan make:controller PostController -r

Go to your Post controller file and paste below code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;use App\Post;class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('add-blog-post-form');
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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');
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

Step 6 — Create Blade File For Form

add-blog-post-form.blade.php

Go to Resources/views/add-blog-post-form.blade.php and create this file

<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Form Example Tutorial laravel Amit</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>

Now refresh the Browser

php artisan serve

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

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

Thanks

Tagged : / / /