this is my code and logic to insert employee data
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Department;
use App\Designation;
use App\Employee;
use App\City;
use App\Country;
use App\State;
use File;
class EmployeeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function Add()
{
$department=Department::get();
$designation=Designation::get();
$employee=Employee::get();
$country=Country::where('country_name','India')->first();
$state=State::where('country_id',$country->country_id)->get();
$city = City::all();
$country=Country::get();
return view('backend.employee.addEmp',compact('department','employee','designation','state','city','country'));
}
public function cityAjax($state)
{
$city=City::where("state_id",$state)
->pluck('cities.city_name','cities.city_id')->all();
return json_encode($city);
}
public function store(Request $request)
{
//dd(request()->all());
$this->validate($request,[
'emp_fname'=>'unique:employees|required',
]);
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')->all();
return json_encode($designation);
}
public function edit( $emp_id)
{
$employee=employee::findOrFail($emp_id);
$department=Department::get();
$designation=designation::get();
$country=Country::where('country_name','India')->first();
$state=State::where('country_id',$country->country_id)->get();
$city=City::where('state_id',$employee->emp_state)->get();
$country=country::get();
//dd($department);
return view('backend.employee.editEmp',compact('employee','department','designation','country','state','city'));
}
public function update(Request $req,Employee $employee)
{
//dd(request()->all());
if($req->hasFile('emp_photo')){
$files=public_path().'/emp_photos/'.$req->input('emp_photo1');
File::delete($files);
//dd($files);
$file = $req->file('emp_photo');
$destinationPath = public_path().'/emp_photos/';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
// echo $filename;
}
else{
$filename=$req->input('emp_photo1');
}
$employee->update([
'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_photo' =>$filename,
]);
return redirect('employee/view');
}
public function destroy(Employee $employee)
{
$files=public_path().'/emp_photos/'.$employee->emp_photo;
File::delete($files);
$employee->delete();
return redirect('employee/view');
}
}