Skip to content

Commit 0c677ca

Browse files
committed
fix: preventing from caching php objects
1 parent b4894bb commit 0c677ca

4 files changed

Lines changed: 27 additions & 25 deletions

File tree

app/Http/Controllers/Api/ShowAllTagsController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ class ShowAllTagsController extends Controller
1313
{
1414
public function __invoke()
1515
{
16-
$tags = Cache::remember(
16+
return Cache::remember(
1717
'inputTags',
1818
now()->addDay(),
19-
fn () => Tag::all()
19+
fn () => TagResource::collection(Tag::all())->resolve()
2020
);
21-
22-
return TagResource::collection($tags);
2321
}
2422
}

resources/views/components/layouts/⚡header.blade.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55
use App\Livewire\Actions\Logout;
66
use App\Models\Category;
77
use App\Services\SettingService;
8-
use Illuminate\Database\Eloquent\Collection;
98
use Illuminate\Support\Facades\Cache;
109
use Livewire\Component;
1110
1211
new class extends Component
1312
{
14-
public Collection $categories;
13+
public array $categories;
1514
1615
public bool $showRegisterButton;
1716
1817
public function mount(): void
1918
{
2019
$this->categories = Cache::remember('categories', now()->addDay(), function () {
21-
return Category::all(['id', 'name', 'icon']);
20+
return Category::all(['id', 'name', 'icon'])->map(fn (Category $category) => [
21+
'id' => $category->id,
22+
'name' => $category->name,
23+
'icon' => $category->icon,
24+
'link_with_name' => $category->link_with_name,
25+
])->toArray();
2226
});
2327
2428
$this->showRegisterButton = SettingService::isRegisterAllowed();
@@ -98,12 +102,12 @@ class="hidden dark:block size-8"
98102

99103
@foreach ($categories as $category)
100104
<x-skew-underline-link
101-
:link="$category->link_with_name"
102-
:selected="urldecode(request()->url()) === urldecode($category->link_with_name)"
103-
:icon="$category->icon"
104-
wire:key="category-{{ $category->id }}"
105+
:link="$category['link_with_name']"
106+
:selected="urldecode(request()->url()) === urldecode($category['link_with_name'])"
107+
:icon="$category['icon']"
108+
wire:key="category-{{ $category['id'] }}"
105109
>
106-
{{ $category->name }}
110+
{{ $category['name'] }}
107111
</x-skew-underline-link>
108112
@endforeach
109113
</nav>
@@ -414,10 +418,10 @@ class="px-2 pt-2 pb-3 space-y-1 lg:hidden"
414418

415419
@foreach ($categories as $category)
416420
@php
417-
$inCategoryPage = urldecode(request()->url()) === urldecode($category->link_with_name);
421+
$inCategoryPage = urldecode(request()->url()) === urldecode($category['link_with_name']);
418422
@endphp
419423
<a
420-
href="{{ $category->link_with_name }}"
424+
href="{{ $category['link_with_name'] }}"
421425
@if ($inCategoryPage) aria-current="page" @endif
422426
@class([
423427
'block px-3 py-2 rounded-md font-medium flex items-center',
@@ -426,8 +430,8 @@ class="px-2 pt-2 pb-3 space-y-1 lg:hidden"
426430
])
427431
wire:navigate
428432
>
429-
<div class="w-4">{!! $category->icon !!}</div>
430-
<span class="ml-2">{{ $category->name }}</span>
433+
<div class="w-4">{!! $category['icon'] !!}</div>
434+
<span class="ml-2">{{ $category['name'] }}</span>
431435
</a>
432436
@endforeach
433437
</nav>

resources/views/components/posts/⚡home-sidebar.blade.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public function render()
1313
{
1414
$popularTags = Cache::remember('popularTags', now()->addDay(), function () {
1515
// 取出標籤使用次數前 20 名
16-
return Tag::withCount('posts')->orderByDesc('posts_count')->limit(20)->get();
16+
return Tag::withCount('posts')->orderByDesc('posts_count')->limit(20)->get()->toArray();
1717
});
1818
1919
$links = Cache::remember('links', now()->addDay(), function () {
20-
return Link::all();
20+
return Link::all()->toArray();
2121
});
2222
2323
return $this->view([
@@ -102,7 +102,7 @@ class="ml-2"
102102
</x-card>
103103

104104
{{-- 熱門標籤 --}}
105-
@if ($popularTags->count())
105+
@if (count($popularTags))
106106
<x-card class="dark:text-zinc-50">
107107
<div class="flex justify-center items-center">
108108
<x-icons.tags class="w-5" />
@@ -113,16 +113,16 @@ class="ml-2"
113113

114114
<div class="flex flex-wrap">
115115
@foreach ($popularTags as $popularTag)
116-
<x-tag :href="route('tags.show', ['id' => $popularTag->id])">
117-
{{ $popularTag->name }}
116+
<x-tag :href="route('tags.show', ['id' => $popularTag['id']])">
117+
{{ $popularTag['name'] }}
118118
</x-tag>
119119
@endforeach
120120
</div>
121121
</x-card>
122122
@endif
123123

124124
{{-- 學習資源推薦 --}}
125-
@if ($links->count())
125+
@if (count($links))
126126
<x-card class="dark:text-zinc-50">
127127
<div class="flex justify-center items-center">
128128
<x-icons.file-earmark-code class="w-5" />
@@ -135,14 +135,14 @@ class="ml-2"
135135
@foreach ($links as $link)
136136
<a
137137
class="flex items-center p-2 rounded-md dark:text-zinc-50 dark:hover:bg-zinc-700 hover:bg-zinc-200"
138-
href="{{ $link->url }}"
138+
href="{{ $link['url'] }}"
139139
target="_blank"
140140
rel="nofollow noopener noreferrer"
141141
>
142142
<span class="flex items-center mr-2 h-lh">
143143
<x-icons.link-45deg class="w-5" />
144144
</span>
145-
{{ $link->title }}
145+
{{ $link['title'] }}
146146
</a>
147147
@endforeach
148148
</div>

resources/views/components/posts/⚡tagify.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
3131
const tagify = window.createTagify(
3232
this.$refs.tags,
33-
tagsList.data
33+
tagsList
3434
);
3535
3636
try {

0 commit comments

Comments
 (0)