CasperSecurity
<?php
namespace App\Http\Livewire\Settings;
use App\Models\SettingsCountry;
use Livewire\Component;
use Livewire\WithPagination;
class CountryLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_name = "Country Informations";
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $country_name, $country_code, $status, $created_by, $modified_by;
public $modelid;
public $showmodal = false;
public $successmsg = "The country data has been saved successfully.";
public $updatemsg = "The country data has been updated successfully.";
public $deletemsg = "The country data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! country 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 = [
'country_name' => 'required|max:255',
'country_code' => 'required|max:255',
];
public $updatedrules = [
'country_name' => 'required|max:255',
'country_code' => 'required|max:255',
'status' => 'required',
];
public $messages = [
'country_name.required' => 'This field is required',
'country_name.max' => 'This field allows maximum 255 character',
'country_code.required' => 'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->resetValidation();
$this->showmodal = true;
//$this->dispatchBrowserEvent('livewire:load');
}
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->country_name = null;
$this->country_code = null;
$this->status = null;
$this->showmodal = false;
}
public function loadData($id)
{
$response = SettingsCountry::find($id);
if ($response) {
$this->country_name = $response['country_name'];
$this->country_code = $response['country_code'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = SettingsCountry::where('country_name', $this->country_name)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->duplicateerrormsg
]);
} else {
$response = SettingsCountry::create([
'country_name' => $this->country_name,
'country_code' => $this->country_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->updatedrules, $this->messages);
$response = SettingsCountry::find($this->modelid);
if ($response) {
$response->update([
'country_name' => $this->country_name,
'country_code' => $this->country_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 = SettingsCountry::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()
{
$depts = SettingsCountry::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $depts;
}
public function render()
{
$datatable = $this->read();
return view('livewire.settings.country-livewire', compact('datatable'));
}
}