I want to list down the users according to the user_types while inserting so I created the two tables and each table has connections. Each model PHP file has a relationship function. and I created the jquery code and I created the controller function but it’s not working I don’t know where I making the mistake please help me to fix this problem. I attached all code I have written and the database also.
User Type Database
User Database Table
UserType Id and User Table usty_id has connections
UserType Model
<?php
namespace Asset_Management_System;
use IlluminateDatabaseEloquentModel;
class UserType extends Model
{
public function userpermission()
{
return $this->hasMany('Asset_Management_SystemUserPermission');
}
public function user()
{
return $this->hasMany('Asset_Management_SystemUser');
}
}
User Model
class User extends Authenticatable
{
public function usertype()
{
return $this->belongsTo('Asset_Management_SystemUserType','usty_id');
}
}
Insert Form
<div class="form-group">
<label>User Type</label>
<select class="form-control select2" style="width: 100%;" id="ust_id" name="ust_id">
<option selected="selected">Select User Type</option>
@foreach($UserType as $ust)
<option value="{{$ust->id}}">{{$ust->usty_name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>User</label>
<select class="form-control select2" style="width: 100%;" id="user_id" name="user_id">
<option selected="selected">Select User</option>
@foreach($User as $us)
<option value="{{$us->id}}">{{$us->us_fname}} {{$us->us_lname}}</option>
@endforeach
</select>
</div>
Controller
public function show(Request $request)
{
//echo $id;
if (!$request->usty_id) {
$html = '<option value="">'.trans('global.pleaseSelect').'</option>';
} else {
$html = '';
$user = User::where('usty_id', $request->usty_id)->get();
foreach ($user as $us) {
$html .= '<option value="'.$us->id.'">'.$us->us_fname.' '.$us->us_lname.'</option>';
}
}
return response()->json(['html' => $html]);
}
And Jquery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$("#ust_id").change(function(){
$.ajax({
url: "{{ route('WorkRequest.show') }}?usty_id=" + $(this).val(),
method: 'GET',
success: function(data) {
$('#user_id').html(data.html);
}
});
});
</script>
Route
Route::get('WorkRequest/show', 'WorkRequestController@show')->name('WorkRequest/show');
And this is the error I’m getting when I go to the form
Missing required parameters for [Route: WorkRequest.show] [URI: WorkRequest/{WorkRequest}]. (View: C:xampphtdocsAsset_Management_Laravelresourcesviewslayoutsmain.blade.php)
Please Help me to solve this issues
2
Answers
This Code is working perfectly
Replace above Route with this-
Route::post('workRequest/get_options','WorkRequestController@getOptions')->name('workRequest.options');
and place above the route::resource() routeAnd ajax code as below
Add this Controller method to get options