Reputation: 31
I store the value of checkbox in the database, the first checkbox(category) value will save correct but second checkbox(sub_category) value stored in the database as null. Category store like 1,2. But subcategory stored null value I want to store the same as a category like 1,2,3.
table_business={id`,`business_id`,`business_name`, `business_pic`,`address`, `email`,`mobile`,`contact_number`,`website`,`business_work_photo`, `category_id`,
`sub_category_id`}
Function
public function insertBusinessDetails(Request $req)
{
$mytime = Carbon::now();
$business_id = DB::table('table_business_registration')->orderby('id', 'DESC')->first();
$created_at = $mytime->toDateTimeString();
$updated_at = $mytime->toDateTimeString();
$name_of_business = $req->input('business_name');
$address = $req->input('address');
$mobile = $req->input('mobile');
$contact_number = $req->input('contact_number');
$category = implode(',', $req->input('category'));
$sub_category = $req->input('sub_category');
$email = $req->input('email');
$website = $req->input('website');
if ($business_id == NULL) {
$query_insert = array(
"business_id" => 1, "business_name" => $name_of_business, "address" => $address, "mobile" => $mobile,
"contact_number" => $contact_number, "email" => $email, "website" => $website, "category_id" => $category,
"sub_category_id" => $sub_category, "updated_at" => $updated_at, "created_at" => $created_at
);
DB::table('table_business_registration')->insert($query_insert);
} else {
$new_business_id = $business_id->business_id + 1;
$query_insert = array(
"business_name" => $name_of_business, "address" => $address, "mobile" => $mobile,
"contact_number" => $contact_number, "email" => $email, "website" => $website, "category_id" => $category,
"sub_category_id" => $sub_category, "updated_at" => $updated_at, "created_at" => $created_at
);
DB::table('table_business_registration')->insert($query_insert);
}
}
Blade
<div class="row mt-3">
<div class="col-sm-6">
<label for="category" class="">Category</label>
</div>
<div class="col-sm-4">
@foreach($category_list as $category)
<div class="form-check form-check-inline">
<input class="form-check-input category" type="checkbox" id="category" name="category[]" value="{{$category->category_id}}">
<label class="form-check-label" for="category" style="float: left;">{{$category->category}}</label>
</div>
@endforeach
</div>
</div>
<div class="row mt-3">
<div class="col-sm-6">
<label for="subcategory" class="">Sub-Category</label>
</div>
<div class="col-sm-4">
<div class="form-check form-check-inline">
<div class="sub_category_of_ multi" id="sub_category" name="sub_category">
</div>
</div>
</div>
</div>
jquery code
<script type="text/javascript">
$(function () {
$('.category').click(function() {
var id = $(this).attr('value');
if (this.checked) {
if (id) {
$.ajax({
type:"GET",
url: "{{url('getSubCategory')}}?category=" + id,
success: function(res) {
if (res) {
$.each(res, function(key, value) {
$("#sub_category").append('<input type="checkbox" value="' + key + '" class="sub_category_of_'+id+'">');
$("#sub_category").append('<label class="sub_category_of_'+id+'">'+ value + '</label>');
});
} else {
// Handle the error case here without removing an existing elements
}
}
});
} else {
// Handle the error case here
}
}
else
{
// Remove the unchecked category checkbox if available
if (id && $('.sub_category_of_'+id).length) {
$('.sub_category_of_'+id).remove();
}
}
});
});
</script>
Upvotes: 0
Views: 246
Reputation: 316
You should add the 'name' attribute to your sub_category check boxes to pass the sub_category value to other end.
Replace your code with below code:
$(function () {
$('.category').click(function() {
var id = $(this).attr('value');
if (this.checked) {
if (id) {
$.ajax({
type:"GET",
url: "{{url('getSubCategory')}}?category=" + id,
success: function(res) {
if (res) {
$.each(res, function(key, value) {
$("#sub_category").append('<input type="checkbox" value="' + key + '" class="sub_category_of_'+id+'" name="sub_category[]"'); // name attribute added
$("#sub_category").append('<label class="sub_category_of_'+id+'">'+ value + '</label>');
});
} else {
// Handle the error case here without removing an existing elements
}
}
});
} else {
// Handle the error case here
}
}
else
{
// Remove the unchecked category checkbox if available
if (id && $('.sub_category_of_'+id).length) {
$('.sub_category_of_'+id).remove();
}
}
});
});
Upvotes: 1
Reputation: 46
Your code is bad. You should use Laravel Relationships for storage in a database information about categories and relations with this. Then you can render categories without additional request by ajax.
About not nulluble in field sub_category
you should't block this becouse then you can get issue when you has not sub_category in category, but if you needed that you can add default value to datebase
About table table_business_registration
you should't do increment by php on field business_id
, you can do this autoincrement in database.
Upvotes: 0