CasperSecurity
<?php
namespace App\Http\Livewire\Settings;
use App\Models\SettingsCity;
use App\Models\SettingsCountry;
use App\Models\SettingsState;
use Livewire\Component;
use Livewire\WithPagination;
class CityLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_name = "City Informations";
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $settings_state_id, $city_name, $city_code, $status;
public $modelId;
public $showmodal = false;
public $successmsg = "The city data has been saved successfully.";
public $updatemsg = "The city data has been updated successfully.";
public $deletemsg = "The city data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! city 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 = [
'settings_state_id' => 'required',
'city_name' => 'required|max:255',
];
public $messages = [
'settings_state_id.required' => 'This field is required',
'city_name.required' => 'This field is required',
'city_name.max' => 'This field allows maximum 255 character',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$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->settings_state_id = null;
$this->city_name = null;
$this->city_code = null;
$this->status = null;
$this->showmodal = false;
}
public function loadData($id)
{
$response = SettingsCity::find($id);
if ($response) {
$this->settings_state_id = $response['settings_state_id'];
$this->city_name = $response['city_name'];
$this->city_code = $response['city_code'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = SettingsCity::create([
'settings_state_id' => $this->settings_state_id,
'city_name' => $this->city_name,
'city_code' => $this->city_code,
]);
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->rules, $this->messages);
$response = SettingsCity::find($this->modelId);
if ($response) {
$response->update([
'settings_state_id' => $this->settings_state_id,
'city_name' => $this->city_name,
'city_code' => $this->city_code,
'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 = SettingsCity::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()
{
$cities = SettingsCity::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $cities;
}
public function render()
{
$dataTables = [];
$dataTables = $this->read();
if (sizeof($dataTables) > 0) {
foreach ($dataTables as $key => $data) {
$sate = SettingsState::select('state_name')->find($data->settings_state_id);
$dataTables[$key]['state_name'] = $sate ? $sate->state_name : 'N/A';
}
}
$states = SettingsState::select('id', 'state_name')->get();
return view('livewire.settings.city-livewire', compact('dataTables', 'states'));
}
}