unioil-cms-fe/app/Livewire/PhotoSlider.php

63 lines
2.0 KiB
PHP

<?php
namespace App\Livewire;
use Carbon\Carbon;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Livewire\Attributes\Layout; // Required for layout declaration
#[Layout('layouts.dashboard')] // Attribute syntax for Laravel 11
class PhotoSlider extends Component
{
public $photoSliders = [];
public function mount()
{
$this->loadPhotoSliders(); // Load users initially
}
public function loadPhotoSliders()
{
try {
$token = Session::get('user')['access_token'] ?? null;
if (!$token) {
$this->addError('users', 'No access token found.');
return;
}
$response = Http::withToken($token)
->get(config('services.backend_api.url') . '/api/cms/photoSlider');
// dd($response->json());
if ($response->successful()) {
// Properly use collect to handle the response data
$this->photoSliders = collect($response->json()['data'])
->map(function ($photoSliders) {
return [
'photoslider_uuid' => $photoSliders['photoslider_uuid'],
'title' => $photoSliders['title'],
'promotion_id' => $photoSliders['type'],
'date_start' => Carbon::parse($photoSliders['date_start'])->format('d-M-Y'),
'date_end' => Carbon::parse($photoSliders['date_end'])->format('d-M-Y'),
];
});
} else {
$this->addError('photo sliders', 'Failed to load photo sliders.');
}
} catch (\Exception $e) {
$this->addError('photo sliders', 'Error: ' . $e->getMessage());
}
}
public function render()
{
return view('livewire.home-page-mobile.photo-slider', [
'photo_slider' => $this->photoSliders, // Pass all users to the table
]);
}
}