Insert Multiple Selected Checkbox Values To Database In Laravel 9

In this article, we will see how to insert multiple selected checkbox values into the database in laravel 9. Here, we will learn how to store multiple checkbox values in the database using jquery in laravel 8 and laravel 9. Sometimes we are required to save multiple checkbox values in the array in laravel.

So, let's see how to store multiple checkbox values in the database using laravel 9, how to store checkbox values in a database in laravel 8 and laravel 9, how to get multiple checkbox values in laravel 9 using an array, laravel 9 stores multiple checkbox values in the database using jquery.

Whenever you want to save multiple checkbox values in a single column in the database at that time this article is helpful for you.

Step 1: Install Laravel 9

Step 2: Configure Database

Step 3: Create Migration and Model

Step 4: Create Route

Step 5: Create Controler

Step 6: Create Blade File

Step 7: Run Laravel 9 Application

 

Step 1: Install Laravel 9

In this step, we will install laravel 9 using the below command to insert multiple selected checkbox values into the database.

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

 

Step 2: Configure Database

In this step, we will configure database details like database name, user name, password, etc.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_9_example
DB_USERNAME=root
DB_PASSWORD=root

 

 

Step 3: Create Migration and Model

 Now, we will create a database migration for the posts table and create a Post model using the artisan command in laravel 9.

php artisan make:model Post -m

After that add the below code to your migration file.

<?php

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

class CreatePostsTable extends Migration
{

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

    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Now, we will add the below code Post model.

app/Models/Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = ['name','category'];

    public function setCategoryAttribute($value)
    {
        $this->attributes['category'] = json_encode($value);
    }

    public function getCategoryAttribute($value)
    {
        return $this->attributes['category'] = json_decode($value);
    }
}

 

Step 4: Create Route

In this step, we will add routes in the route.php file.

web/route.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class);

 

Step 5: Create Controler

Now, we will create a PostController.php file. So, add the following code to that file.

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::all();
        return view('index',compact('posts'));
    }

    public function create()
    {
        return view('multiple_checkbox');
    }

    public function store(Request $request)
    {
        $input = $request->all();
        $input['category'] = $request->input('category');
        Post::create($input);
        return redirect()->route('posts.index');
    }
}

 

 

Step 6: Create Blade File

In this step, we will create an index.blade.php file. So, add the following code to that file.

resources/views/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Insert Multiple Selected Checkbox Values To Database In Laravel 9 - Techsolutionstuff</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha256-aAr2Zpq8MZ+YA/D6JtRD3xtrwpEz2IqOS+pWD/7XKIw=" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha256-OFRAJNoaD8L3Br5lglV7VyLRf0itmoBzWUoM+Sji4/8=" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-3 mt-5">
                <div class="card">
                    <div class="card-header bg-info">
                        <h6 class="text-white">Insert Multiple Selected Checkbox Values To Database In Laravel 9 - Techsolutionstuff</h6>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-12 text-right mb-3">
                                <a href="{{ route('posts.create') }}" class="btn btn-success">Create</a>
                            </div>
                        </div>
                        <table class="table table-bordered">
                            <tr>
                                <th>Name</th>
                                <th>Category</th>
                            </tr>
                            @foreach($posts as $post)
                            <tr>
                                <td>{{ $post->name }}</td>

                                <td>
                                    @foreach($post->category as $value)
                                        {{$value}},
                                    @endforeach
                                </td>
                            </tr>
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

resources/views/multiple_checkbox.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Store Multiple Checkbox Value In Database - Techsolutionstuff</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha256-aAr2Zpq8MZ+YA/D6JtRD3xtrwpEz2IqOS+pWD/7XKIw=" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha256-OFRAJNoaD8L3Br5lglV7VyLRf0itmoBzWUoM+Sji4/8=" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-3 mt-5">
                <div class="card">
                    <div class="card-header bg-info">
                        <h6 class="text-white">Laravel 9 Store Multiple Checkbox Value In Database - Techsolutionstuff</h6>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-12 text-right mb-3">
                                <a href="{{ route('posts.index') }}" class="btn btn-primary">Back</a>
                            </div>
                        </div>
                        <form method="post" action="{{ route('posts.store') }}" enctype="multipart/form-data">
                            @csrf
                            <div class="form-group">
                                <label><strong>Name :</strong></label>
                                <input type="text" name="name" class="form-control"/>
                            </div>  
                            <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="PHP"> PHP</label>
                                <label><input type="checkbox" name="category[]" value="MySQL"> MySQL</label>
                                <label><input type="checkbox" name="category[]" value="jquery"> jQuery</label>
                            </div>  

                            <div class="form-group text-center">
                                <button type="submit" class="btn btn-success btn-sm">Save</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

Step 7: Run Laravel 9 Application

Now, we will laravel 9 insert multiple selected checkbox values to the database using the following command,

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS