CasperSecurity
<?php
namespace App\Http\Livewire\Transportmanagement;
use App\Models\Designation;
use App\Models\EmployeeRegistration;
use App\Models\ManageDriver;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
class ManageDriverLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $employee_id;
public $driver_id,$driver_licence_no,$driver_license_expiry_date,$driver_license_docs;
public $modelid;
public $showmodal=false;
public $status=true;
public $successmsg="Driver has been created successfully.";
public $updatemsg="Driver has been updated successfully.";
public $deletemsg="Driver has been deleted successfully";
public $duplicateerrormsg="Driver has been already registered";
public $errormsg="Oops !!! Something went wrong.";
protected $listeners = ['delete'];
protected $rules = [
'driver_id' => 'required',
'driver_licence_no' => 'required',
'driver_license_expiry_date' => 'required|date',
'driver_license_docs' => 'required',
'status' => 'nullable',
];
protected $updatedrules = [
'employee_id' => 'required|exists:drivers,id', // or whatever is appropriate
'driver_licence_no' => 'required|unique:manage_drivers',
'driver_license_expiry_date' => 'required|date',
'status'=>'nullable',
];
protected $messages = [
'driver_id.required' => 'The Driver Name cannot be empty.',
'driver_licence_no.required' => 'The Driver Licence No cannot be empty.',
'driver_license_expiry_date.required' => 'The Driver Licence Expiry Date cannot be empty.',
'driver_license_docs.required' => 'The Driver License Document cannot be empty.',
'status.required' => 'The status cannot be empty.',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->cleanVar();
$this->resetValidation();
$this->showmodal=true;
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
}
public function updateclick($id){
$this->modelid=$id;
$this->loadData($this->modelid);
$this->showmodal=true;
}
public function deleteclick($id){
$this->modelid=$id;
$this->dispatchBrowserEvent('confirmdelete',[
'message'=>'Are you sure want to delete this data ?',
'funcname'=>'delete'
]);
}
public function cleanVar(){
$this->modelid=null;
$this->driver_id=null;
$this->driver_license_expiry_date=null;
$this->status=null;
$this->showmodal=false;
}
public function loadData($id){
$response=ManageDriver::find($id);
if($response){
$this->driver_id =$response['driver_id'];
$this->driver_licence_no =$response['driver_licence_no'];
$this->driver_license_expiry_date =$response['driver_license_expiry_date'];
$docs = $response->driver_license_docs;
$this->driver_license_docs = $docs;
$this->status=$response['status'];
}
}
public function save()
{
$this->validate($this->rules,$this->messages);
$response= ManageDriver::where('driver_id',$this->driver_id)
->orWhere('driver_licence_no', $this->driver_licence_no)
->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else {
$response = ManageDriver::create([
'driver_id' => $this->driver_id,
'driver_licence_no' => $this->driver_licence_no,
'driver_license_expiry_date' => $this->driver_license_expiry_date,
'driver_license_docs' => $this->driver_license_docs->store('manage_drivers','public'),
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg,
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg,
]);
}
}
}
public function update(){
$this->validate($this->updatedrules,$this->messages);
$response=ManageDriver::find($this->modelid);
if ($response) {
$docs = $this->driver_license_docs ? $this->driver_license_docs->store('manage_drivers','public') : $response->driver_license_docs;
$response->update([
'driver_id' => $this->driver_id,
'driver_licence_no' => $this->driver_licence_no,
'driver_license_expiry_date' => $this->driver_license_expiry_date,
'driver_license_docs' =>$docs,
'status'=>$this->status,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=ManageDriver::find($this->modelid);
if($response){
if($response->delete()){
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->deletemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function read(){
$manage_drivers = ManageDriver::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $manage_drivers ;
}
public function render()
{
$datatable=[];
$datatable=$this->read();
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$driver = EmployeeRegistration::where('id',$data->driver_id)->first();
if($driver){
$datatable[$key]['name'] =$driver->name;
}
else{
$datatable[$key]['name'] ="NA";
}
}
}
$designation = Designation::where('designation_name', 'Driver')->first();
if ($designation) {
$drivers = EmployeeRegistration::where('designation', $designation->id)->get();
} else {
$drivers = collect();
}
return view('livewire.transportmanagement.manage-driver-livewire',compact('datatable','drivers','designation'));
}
}