Ravindra24.com

How to checkbox get selected from database table

In this tutorial I am going to explain how can we get selected checkbox automatically by checking the recording into database table in Laravel. In this tutorial program will checker whether the value of checkbox is already in table or not, If the value is already available in database table it will be checked and if is not available in table it will remain unchecked.so following are the step which you need to follow to make this working.

Step 1

First, You need to create a Laravel project with following artisan command

composer create-project laravel/laravel testPro

testPro is the project name and your project folder name is also testPro

Step 2

Now, Create a database and Table of it.You can specify

In this table you can see there is a column called “cat_name” the ckeckboxed will be compared with this column of table.

Step 3

Now, Create a blade file under resources folder and create controller, routes for your project. In this Blade file you are going to fetch the data from database table.

Step 4

Following is the code to fetch and display the categories from the database table in Blade file of your project.

@foreach($category as $cat)
<input type="checkbox" name="category[]" value="{{$cat->id}}" @if(Str::contains($rec->category, $cat->id)) checked @endif /> {{$cat->cat_name}}<br />
@endforeach

Str::contains() function is used to check weather the category id is already in records or not if it is available in the records, it will be checked otherwise it will remain unchecked.

Leave a Comment