cms-laravel/app/Livewire/FetchData.php

48 lines
933 B
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
use App\Services\ApiService;
use Illuminate\Support\Facades\Session;
class FetchData extends Component
{
public $data = [];
public $loading = false;
public $url;
protected $apiService;
public function boot(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function mount($url)
{
$this->url = $url;
$this->fetchData();
}
public function fetchData()
{
$this->loading = true;
try {
$data = $this->apiService->fetchData($this->url);
if ($data) {
$this->data = $data;
}
} catch (\Exception $e) {
Session::flash('error', $e->getMessage());
} finally {
$this->loading = false;
}
}
public function render()
{
return view('livewire.fetch-data');
}
}