Laravel Store Multiple Checkbox Value Example; In this tutorial how to store multiple checkbox value in database with laravel application.
Here we will utilize multiple checkbox value stores in the database used json_encode and json-decode. Insert data JSON encode value and get data to JSON decode in laravel app.
Below we have implemented the full example for store multiple checkbox values in the database utilizing laravel. Follow the following step by step guide on How To Store Multiple Checkbox Value In Database Using Laravel.
Step 1 : Install Laravel App
In this first step, you have to use the following command to create a new laravel project:
composer create-project laravel/laravel laravel-app --prefer-dist
See Here: Install Laravel and Basic Configuration
Step 2: Connect App to Database
Get into laravel project root, open .env file and update the database configuration details as given below:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databse
DB_USERNAME=root
DB_PASSWORD=
Step 3: Create Model and Run Migration
Further, create a model and migration file using running the following command; This command will generate you a new model and migration file;
php artisan make:model Post -m
Define code in database/migrations/create_posts_uploads_table.php:
<?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->json('category')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Finally invoke the database migration:
php artisan migrate
Add code as given below in app/Models/Post.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'description', 'category'
];
}
Step 4: Make Routes
Define following code in routes/web.php file to create routes:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
Step 5: Create Controller
In this step you have to generate a controller using the followin command:
php artisan make:controller PostController
Add following code in app\Http\Controllers\PostController.php.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$posts = Post::orderBy('id', 'desc')->paginate(10);
return view('posts',compact('posts'));
}
public function create()
{
return view('checkbox-form');
}
public function store(Request $request)
{
Post::create([
'title' => $request->title,
'description' => $request->description,
'category' => json_encode($request->category)
]);
return redirect()->route('posts.index');
}
}
Step 6: Create Blade Files
In this step we have to two create view file. first one is list display file and second one is form file.
resources\views\posts.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>How To Store Multiple Checkbox Value In Database Using Laravel</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>How To Store Multiple Checkbox Value In Database Using Laravel </h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('posts.create') }}"> Add New Post</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Description</th>
<th>Categories</th>
</tr>
@foreach ($posts as $post)
<tr>
<td>{{ $post->title }}</td>
<td>{{ $post->description }}</td>
<td>
@php $categories = $post->category ? json_decode($post->category, true) : []; @endphp
@foreach($categories as $category)
{{$category}},
@endforeach
</td>
</tr>
@endforeach
</table>
</div>
</body>
</html>
Create a new file ‘checkbox-form.blade.php’ file, then put the below code on it.
resources\views\checkbox-form.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>How To Store Multiple Checkbox Value In Database Using Laravel</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Post</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a>
</div>
</div>
</div>
<form action="{{ route('posts.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
<input type="text" name="title" class="form-control" placeholder="Title">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
<textarea class="form-control" rows="5" name="description" placeholder="Description"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<label><strong>Category :</strong></label><br>
<label><input type="checkbox" name="category[]" value="Laravel"> Laravel</label>
<label><input type="checkbox" name="category[]" value="JQuery"> JQuery</label>
<label><input type="checkbox" name="category[]" value="Bootstrap"> Bootstrap</label>
<label><input type="checkbox" name="category[]" value="Codeigniter"> Codeigniter</label>
<label><input type="checkbox" name="category[]" value="JQuery UI"> JQuery UI</label>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</body>
</html>
Now we are ready to run our example so run bellow command for quick run:
php artisan serve
Now copy the below url and paste in your browser then test your requirements…
http://localhost:8000/posts
I hope this example help you…