File: /var/www/admin.fixgini.com/app/Livewire/Country/All.php
<?php
namespace App\Livewire\Country;
use App\Models\Country;
use Livewire\Component;
use Livewire\WithPagination;
class All extends Component
{
use WithPagination;
public $search = '';
public $sortField = 'name';
public $sortDirection = 'asc';
public $perPage = 10;
public function sortBy($field)
{
$this->sortDirection = $this->sortField === $field
? ($this->sortDirection === 'asc' ? 'desc' : 'asc')
: 'asc';
$this->sortField = $field;
}
public function updatingSearch()
{
$this->resetPage();
}
public function changeCountryStatus($countryId, $status)
{
// Validate the status input to be either 'active' or 'inactive'
if (!in_array($status, ['active', 'inactive'])) {
session()->flash('error', 'Invalid status.');
return $this->redirect(request()->header('Referer'), navigate: true);
}
// Find the review by ID and update its status
$country = Country::find($countryId);
if ($country) {
$country->status = $status;
$country->save();
// Optionally, flash a success message to the session
session()->flash('success', 'Country status updated successfully!');
return $this->redirect(request()->header('Referer'), navigate: true);
} else {
session()->flash('error', 'Country not found.');
}
}
public function deleteCountry($countryId)
{
$country = Country::find($countryId);
if ($country) {
$country->delete();
return $this->redirect(request()->header('Referer'), navigate: true);
} else {
session()->flash('error', 'country not found');
return $this->redirect(request()->header('Referer'), navigate: true);
}
}
public function render()
{
return view('livewire.country.all', [
'countries' => Country::query()
->with(['states', 'cities'])
->where(function ($query) { // Group the search conditions together
$query->where('name', 'like', '%' . $this->search . '%');
})
->orderBy($this->sortField, $this->sortDirection)
->paginate($this->perPage),
]);
}
}