Level 75
Leave out segment in route, just get it using a request. As in get request.
view
<form class="m-t-40" method="post" action="{{url('/')}}/mechanic_brands/{{$mechanic->mechanic_id}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<h5>Segment<span class="text-danger">*</span></h5>
<div class="controls">
@foreach($segment as $sg)
<ul><li>
<input type="checkbox" value="{{$sg->segment_id }}" name="sg[{{$sg->segment_id}}]" id="{{$sg->segment_id }}" onchange="getBrand(this.value)">
<label for="{{ $sg->segment_id }}">
<p>{{$sg->segment_name }}</p></label></li></ul>
@endforeach
</div>
</div>
<div id="brand"></div>
<!-- <div class="form-group">
<h5>Brands<span class="text-danger">*</span></h5>
<div class="controls">
@foreach($brand as $br)
<ul><li>
<input type="checkbox" name="br[{{$br->brand_id}}]" value ="{{$br->brand_id}}" id="{{$br->brand_id }}" class="test">
<label for="{{ $br->brand_id }}">
<p><img src="{{url('/')}}/brand_logos/{{$br->brand_logo}}" width="70px" height="40px" /></p></label></li></ul>
@endforeach
</div>
</div> -->
<div class="text-xs-right">
<button type="submit" class="btn btn-info">Submit</button>
<button type="reset" class="btn btn-inverse">Cancel</button>
</div>
</form>
script
function getBrand(segment)
{
if(segment) {
$.ajax({
url: '{{url('/')}}/brand/Ajax/'+segment,
type: "GET",
dataType: "json",
success:function(result) {
data = eval(result);
var options = '';
$.each(data, function(value,key){
options += '<input type="checkbox" name="brand_id-' + key + '" id="brand_id-' + key + '" value="' + key + '" class="controls" />';
options += '<label for="brand_id-' + key + '">' + value + '</label>';
});
$('#brand').append(options);
}
});
return false;
};
}
controller
public function brandAjax($segment)
{
$brand=brands::where("segment_id",$segment)
->pluck('brands.brand_name,brands.brand_id')->all();
return json_encode($brand);
}
web.php
//get segment wise brand using ajax
Route::get('brand/Ajax/{segment}','MechBrandController@brandAjax');
@kanchan186 in your AJAX, change the url to this instead:
url: "{{url('/')}}/brand/Ajax/"+segment,
You are using wrong quotes otherwise.
Make sure that this.value has a value in it by printing it in the console using:
console.log(segment);
Please or to participate in this conversation.