inchirags@gmail.com Chirag's Laravel Tutorial https://www.chirags.in
*********************************************************************************************
How To Store Multiple Checkbox Values in Database and Show Data on View In Laravel 11
*********************************************************************************************
YouTube Video:
Here's a step-by-step guide to store multiple checkbox values in the database and display the data in a view using Laravel 11:
Step 1: Set Up Laravel Project
Ensure you have Laravel 11 installed and set up.
composer create-project --prefer-dist laravel/laravel laravel_11_multicheckbox
cd laravel_11_multicheckbox
php artisan serve
Open the application in your browser at
http://127.0.0.1:8000
Step 2: Set Up the Database
Configure the database in .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_11_multicheckbox
DB_USERNAME=root
DB_PASSWORD=admin@123
Replace admin@123 with your DB_PASSWORD.
Create a new database:
CREATE DATABASE laravel_11_checkbox;
Step 3: Create a Model and Migration
Generate a model with a migration for storing checkbox values.
php artisan make:model Item -m
Open the migration file in database/migrations/xxxx_xx_xx_create_items_table.php and define the schema:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->json('options'); // For storing checkbox values as JSON
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('items');
}
};
Run the migration:
php artisan migrate
Step 4: Edit model page (add fillable).
// App\Models\Item.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $fillable = ['name','options'];
}
Step 5: Create a Controller
Generate a controller:
php artisan make:controller ItemController
Add methods for storing and displaying data:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Item;
class ItemController extends Controller
{
public function create()
{
return view('items.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'options' => 'required|array', // Ensure 'options' is an array
]);
Item::create([
'name' => $request->name,
'options' => json_encode($request->options), // Convert array to JSON
]);
return redirect()->route('items.index');
}
public function index()
{
$items = Item::all();
return view('items.index', compact('items'));
}
}
Step 6: Define Routes
Open routes/web.php and define routes:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ItemController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/items/create', [ItemController::class, 'create'])->name('items.create');
Route::post('/items/store', [ItemController::class, 'store'])->name('items.store');
Route::get('/items', [ItemController::class, 'index'])->name('items.index');
Step 7: Create Blade Views
1. Create Form View (resources/views/items/create.blade.php):
<!DOCTYPE html>
<html>
<head>
<title>Add Item - Chirags.in</title>
</head>
<body>
<h1>Add Item</h1>
<a href="{{ route('items.index') }}">Items List</a>
<form action="{{ route('items.store') }}" method="POST">
@csrf
<label for="name">Item Name:</label>
<input type="text" name="name" id="name" required>
<br><br>
<label>Options:</label><br>
<input type="checkbox" name="options[]" value="Option 1"> Option 1<br>
<input type="checkbox" name="options[]" value="Option 2"> Option 2<br>
<input type="checkbox" name="options[]" value="Option 3"> Option 3<br>
<br>
<button type="submit">Submit</button>
</form>
</body>
</html>
2. Display Data View (resources/views/items/index.blade.php):
<!DOCTYPE html>
<html>
<head>
<title>Item List - Chirags.in</title>
</head>
<body>
<h1>Item List</h1>
<a href="{{ route('items.create') }}">Add New Item</a>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Options</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
<td>
@foreach(json_decode($item->options) as $option)
{{ $option }}<br>
@endforeach
</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
Step 8: Test the Application
Access the form at
http://127.0.0.1:8000/items/create
and submit data with multiple checkboxes selected.
View the stored data at
http://127.0.0.1:8000/items
Summary
Model: Handles data storage and retrieval.
Migration: Creates a schema to store checkbox values as JSON.
Controller: Manages data flow between views and the database.
Blade Views: Provide user interfaces for form submission and data display.
Routes: Define endpoints for accessing views and handling form submissions.
For any doubts and query, please write on YouTube video comments section.
Note : Flow the Process shown in video.
Please, Subscribe and like for more videos:
https://www.youtube.com/@chiragstutorial
Don't forget to, Follow, Like, Share &, Comment
Thanks & Regards,
Chitt Ranjan Mahto "Chirag"
_____________________________________________________________________
Note: All scripts used in this demo will be available in our website.
Link will be available in description.
#chirags
#chiragstutorial
#chiragslaraveltutorial
#chiragslaraveltutorials
#laraveltutorial
#laravel11
#laravelcourse
#installlaravel
#laravel_tutorial
#laravelphp
#chiragdbatutorial
#chiragsdbatutorial
#chriagstutorial
#laravel11multicheckbox
chirags, chirags tutorial, chirags laravel tutorial, chirags Laravel tutorial, Laravel tutorial, laravel11, Laravel course, install laravel, laravel_tutorial, Laravel php, chirags dba tutorial, chirags tutorial, Laravel 11 Multi Checkbox