CasperSecurity
<?php
namespace App\Http\Livewire\Settings;
use App\Models\SettingsCountry;
use App\Models\SettingsState;
use Hamcrest\Core\Set;
use Livewire\Component;
use Livewire\WithPagination;
use function Symfony\Component\Finder\size;
class StateLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_name = "State Informations";
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $settings_country_id, $state_name, $state_code, $status;
public $modelId;
public $showmodal = false;
public $successmsg = "The state data has been saved successfully.";
public $updatemsg = "The state data has been updated successfully.";
public $deletemsg = "The state data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! state 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_country_id' => 'required',
'state_name' => 'required|max:255',
];
public $messages = [
'settings_country_id.required' => 'This field is required',
'state_name.required' => 'This field is required',
'state_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_country_id = null;
$this->state_name = null;
$this->state_code = null;
$this->status = null;
$this->showmodal = false;
}
public function loadData($id)
{
$response = SettingsState::find($id);
if ($response) {
$this->settings_country_id = $response['settings_country_id'];
$this->state_name = $response['state_name'];
$this->state_code = $response['state_code'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = SettingsState::where('state_name', $this->state_name)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->duplicateerrormsg
]);
} else {
$response = SettingsState::create([
'settings_country_id' => $this->settings_country_id,
'state_name' => $this->state_name,
'state_code' => $this->state_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 = SettingsState::find($this->modelId);
if ($response) {
$response->update([
'settings_country_id' => $this->settings_country_id,
'state_name' => $this->state_name,
'state_code' => $this->state_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 = SettingsState::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()
{
$states = SettingsState::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $states;
}
public function render()
{
$dataTables = [];
$dataTables = $this->read();
if (sizeof($dataTables) > 0) {
foreach ($dataTables as $key => $data) {
$country = SettingsCountry::select('country_name')->find($data->settings_country_id);
$dataTables[$key]['country_name'] = $country ? $country->country_name : 'N/A';
}
}
$countries = SettingsCountry::select('id', 'country_name')->get();
return view('livewire.settings.state-livewire', compact('dataTables', 'countries'));
}
}