please check my code where i going wrong
Controller
public function store(Request $request)
{
//dd(request()->all());
$this->validate($request,[
'emp_fname'=>'unique:employees|required',
'designation_id' => 'exists:designations,department_id' // checks if the is a record matching the provided value
]);
if($request->hasFile('emp_photo')){
$file = $request->file('emp_photo');
$destinationPath = public_path().'/emp_photos/';
$filename = $file->getClientOriginalName();
if(!File::exists($destinationPath))
{
File::makeDirectory($destinationPath, $mode = 0777, true);
$file->move($destinationPath, $filename);
}
else
{
$file->move($destinationPath, $filename);
}
// echo $filename;exit;
}//end of if
$emp_id=employee::create([
//'emp_id' => request('emp_id'),
'emp_photo' =>$filename,
'emp_fname' => request('emp_fname'),
'emp_lname' => request('emp_lname'),
'emp_email' => request('emp_email'),
'emp_bdate' => request('emp_bdate'),
'emp_mobile' => request('emp_mobile'),
'emp_alt_mobile' => request('emp_alt_mobile'),
'emp_country' => request('emp_country'),
'emp_state' => request('emp_state'),
'emp_city' => request('emp_city'),
'emp_address' => request('emp_address'),
'emp_pincode' => request('emp_pincode'),
'gender' => request('gender'),
'department_id' => request('department_id'),
'designation_id' => request('designation_id'),
'emp_id_proof' => request('emp_id_proof'),
'emp_username' => request('emp_username'),
'password' => request('password'),
'login_first'=>request('login_first'),
'report_to_id' => request('report_to_id'),
'login_first' => 0,
]);
return redirect('employee/view');
}
public function show()
{
$employee=employee::join('departments','employees.department_id','=','departments.department_id')
->join('designations','employees.designation_id','=','designations.designation_id')
->get();
$department=Department::get();
$designation=designation::get();
//return $names;
return view('backend.employee.viewEmp',compact( 'department','employee'));
}
public function designationAjax($department)
{
$designation = Designation::where("department_id",$department)
->pluck('designations.designation_name','designations.designation_id')->all();
return json_encode($designation);
}
view
<div class="form-group">
<h5>Select Department <span class="text-danger">*</span></h5>
<div class="controls">
<select class="form-control" name="department_id" onchange="getDesignation(this.value)" required>
<option>--Select Department--</option>
@foreach($department as $br)
<option value="{{$br->department_id}}">{{$br->department_name}}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<h5>Select Designation <span class="text-danger">*</span></h5>
<div class="controls">
<select class="form-control " name="designation_id" required>
<option>--Select Designation--</option>
</select>
</div>
</div>
script
function getDesignation(department)
{
if(department) {
$.ajax({
url: '{{url('/')}}/designation/ajax/'+department,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="designation_id"]').empty();
$('select[name="designation_id"]').prepend('<option value="">--Select designation--</option>');
$.each(data, function(key, value) {
$('select[name="designation_id"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
} else{
$('select[name="designation_id"]').empty();
}
}