Step 1: Update Database Credentials
Open your Laravel project's .env
file and update the database settings:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_learn DB_USERNAME=root DB_PASSWORD=
Step 2: Checkbox Form UI (Blade Template)
Create a Blade view (e.g., resources/views/form.blade.php
) and paste this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Checkbox Form</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<style>
body {
font-family: 'Roboto', sans-serif;
background: #f5f5f5;
color: #999;
}
.signup-form {
width: 50%;
margin: auto;
padding: 30px;
}
.form-check-label {
margin-left: 10px;
}
</style>
</head>
<body>
<div class="signup-form">
<h2 class="text-center">Select Programming Languages</h2>
<form action="{{ route('form/save') }}" method="POST">
@csrf
<div class="row">
<div class="col-sm">
<h5>Front-End</h5>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="HTML"> HTML
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="CSS"> CSS
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="JS"> JS
</div>
</div>
<div class="col-sm">
<h5>Back-End</h5>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="PHP"> PHP
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="JAVA"> JAVA
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="ASP.NET"> ASP.NET
</div>
</div>
<div class="col-sm">
<h5>Other</h5>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="Laravel"> Laravel
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="Python"> Python
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkbox[]" value="Node.js"> Node.js
</div>
</div>
</div>
<br>
<div class="text-center">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</form>
</div>
</body>
</html>
Step 3: Controller Logic
In your app/Http/Controllers/FormController.php
:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class FormController extends Controller
{
public function index()
{
return view('form');
}
public function saveRecord(Request $request)
{
foreach ($request->checkbox as $value) {
DB::table('form_checkbox_tbls')->insert([
'name_program' => $value,
'created_at' => now(),
'updated_at' => now(),
]);
}
return redirect()->back()->with('success', 'Selections saved successfully!');
}
}
Step 4: Web Routes
In routes/web.php
:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FormController;
Route::get('/', fn() => redirect('form/new'));
Route::get('form/new', [FormController::class, 'index'])->name('form/new');
Route::post('form/save', [FormController::class, 'saveRecord'])->name('form/save');
Step 5: Migration
Generate and update the migration file (or create a new one manually):
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFormCheckboxTblsTable extends Migration
{
public function up()
{
Schema::create('form_checkbox_tbls', function (Blueprint $table) {
$table->id();
$table->string('name_program')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('form_checkbox_tbls');
}
}
Then run:
php artisan migrate
Step 6: Run Your Laravel App
php artisan serve
Visit: http://localhost:8000/form/new