view
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title">Add Mechanic Brands</h4>
<h6 class="card-subtitle"></h6>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<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 }}" onclick="getBrand1()">
<label for="{{ $sg->segment_id }}">
<p>{{$sg->segment_name }}</p></label></li></ul>
@endforeach
</div>
</div>
<div class="controls" id="brands">
</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>
</div>
</div>
</div>
</div>
script
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
function getBrand1()
{
alert('Choose brands!');
if(document.getElementById("segment_id").checked)
{
$.ajax({
url: '/brand/Ajax/'+segment,
type: "GET",
dataType: "json",
success:function(result) {
data = eval(result);
$.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>'; });
$("#brands").html(response);
}
});
}
else
{
$("#brands").html("");
}
}
</script>
@stop
controlller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mechanic;
use App\Segment;
use App\Brands;
use App\MechBrand;
class MechBrandController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function Add($mechanic_id)
{
$mechanic=Mechanic::findOrFail($mechanic_id);
// $mechanic_service_station_info=MechanicServiceStationInfo::get();
$segment=Segment::get();
$brand=Brands::get();
$mech_brand=MechBrand::where('mech_brands.mechanic_id',$mechanic_id)->first();
//dd($state);
return view('backend.mechanic_brands.addMechBrands',compact('mechanic','brand','segment'));
}
public function brandAjax($segment)
{
$brand=brands::where("segment_id",$segment)
->pluck('brand_name','brand_id')->all();
return json_encode($brand);
}
public function store(Request $request)
{
foreach(request('sg') as $sg_id)
{
foreach(request('br') as $br_id)
{
$brands=Brands::where('brands.segment_id', $sg_id)->first();
if( $sg_id==$brands->segment_id)
{
MechBrand::create([
'mechanic_id'=>request('mechanic_id'),
'active_status'=>1,
'segment_id'=>$sg_id,
'brand_id' => $br_id,
]);
}
}
}
$mechanic=Mechanic::get();
$segment=Segment::get();
$brand=Brands::get();
array($request->get('segment_id'));
array($request->get('brand_id'));
return redirect('mechanic/view');
}
}
web.php
//get segment wise brand using ajax
Route::get('brand/Ajax/{segment}','MechBrandController@brandAjax');