cms-laravel/app/Livewire/StationLocator/BranchList.php

58 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\StationLocator;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
class BranchList extends Component
{
public $branches = [];
public $updating = false;
public function mount()
{
$this->fetchBranches();
}
public function fetchBranches()
{
try {
$response = Http::get('https://api.example.com/branches');
if ($response->successful()) {
$this->branches = $response->json()['data'] ?? [];
} else {
$this->alert('error', 'Failed to fetch branches.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
}
}
public function delete($id)
{
if (confirm('Are you sure you want to delete this branch?')) {
try {
$this->updating = true;
$response = Http::delete("https://api.example.com/branches/{$id}");
if ($response->successful()) {
$this->alert('success', 'Branch deleted successfully!');
$this->fetchBranches();
} else {
$this->alert('error', 'Failed to delete branch.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
} finally {
$this->updating = false;
}
}
}
public function render()
{
return view('livewire.station-locator.branch-list')->layout('layouts.app', ['title' => 'Branch List']);
}
}