Within my Laravel-5.8 project, I have implemented the following JSON get request code in the controller:
public function findIdentity(Request $request)
{
$userCompany = Auth::user()->company_id;
$identity = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('id',$request->id)->first();
return response()->json([
'identity' => $identity->id
]);
}
View blade:
< script type = "text/javascript" >
$(document).ready(function() {
$(document).on('change', '#identity', function() {
var air_id = $(this).val();
var a = $(this).parent();
var op = "";
$.ajax({
type: 'get',
url: '{{ route('get.identities.all') }}',
data: {
'id': air_id
},
dataType: 'json', //return data will be json
success: function(data) {
console.log(data.identity);
$('#identity_id').val(data.identity);
},
error: function() {
}
});
});
}); <
/script>
<div class="row" style="margin-bottom: 10px">
<label class="control-label"> Performance Period:<span style="color:red;">*</span></label>
<div class="col-sm-8">
<select id="identity" name="identity_id" class="form-control">
<option value="">--- Select Performance Period ---</option>
@foreach ($identities as $ids => $name)
<option value="{{ $ids }}" {{ request('identity_id')==$ids ? 'selected' : ''}}>{{ $name }}</option>
@endforeach
</select>
</div>
<input type="hidden" id="identity_id" class="form-control">
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-sm" name="search">Search</button>
<form action="{{ route('performance_dashboard', $id) }}" method="get" target="_blank" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<button type="submit" class="btn btn-secondary btn-sm" name="viewIdentity"><i class="fas fa-file-pdf"></i> View Identity</button>
</form>
</div>
</div>
route:
Route::get('get/findIdentity','HomeController@findIdentity')->name('get.identities.all');
In the context of all the provided codes, my objective is to pass the #identity_id as the parameter #id in the following expression:
{{ route('performance_dashboard', $id) }}