CasperSecurity
<?php
namespace App\Http\Livewire\Transportmanagement;
use App\Models\VehicleType;
use Livewire\Component;
use Livewire\WithPagination;
class VehicleTypeLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $vehicle_type, $status, $data;
public $modelid;
public $showmodal = false;
public $successmsg = "The Vehicle Type data has been saved successfully.";
public $updatemsg = "The Vehicle Type data has been updated successfully.";
public $deletemsg = "The Vehicle Type data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Vehicle Type name has already exists in our database. Please Try Another Name";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'vehicle_type' => 'required|',
'status' => 'nullable',
];
public $updatedrules = [
'vehicle_type' => 'required|',
'status' => 'required',
];
public $messages = [
'vehicle_type.required' => 'This field is required',
'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->vehicle_type=null;
$this->status=null;
}
public function loadData($id){
$response=VehicleType::find($id);
if($response) {
$response = VehicleType::find($id);
if ($response) {
$this->vehicle_type = $response['vehicle_type'];
$this->status = $response['status'];
}
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = VehicleType::where('vehicle_type', $this->vehicle_type)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->duplicateerrormsg
]);
} else {
$response = VehicleType::create([
'vehicle_type' => $this->vehicle_type,
]);
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 = VehicleType::find($this->modelid);
if ($response) {
$response->update([
'vehicle_type' => $this->vehicle_type,
'status' => $this->status,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function delete()
{
$response = VehicleType::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()
{
$vehicle_types = VehicleType::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $vehicle_types;
}
public function render()
{
$datatable=$this->read();
return view('livewire.transportmanagement.vehicle-type-livewire',compact('datatable'));
}
}