|
8 | 8 | use Illuminate\Support\Facades\Http; |
9 | 9 | use Illuminate\Support\Str; |
10 | 10 | use Inertia\Inertia; |
| 11 | +use Spatie\Tags\Tag; |
11 | 12 |
|
12 | 13 | class ThemesController extends Controller |
13 | 14 | { |
@@ -85,13 +86,13 @@ public function store(Request $request) |
85 | 86 | } |
86 | 87 | } |
87 | 88 |
|
88 | | - if (isset($data['categories'])) { |
89 | | - if (! is_array($data['categories'])) { |
90 | | - $errors[] = '"categories" must be an array.'; |
| 89 | + if (isset($data['tags'])) { |
| 90 | + if (! is_array($data['tags'])) { |
| 91 | + $errors[] = '"tags" must be an array.'; |
91 | 92 | } else { |
92 | | - foreach ($data['categories'] as $i => $category) { |
93 | | - if (! is_string($category)) { |
94 | | - $errors[] = "\"categories.{$i}\" must be a string."; |
| 93 | + foreach ($data['tags'] as $i => $tag) { |
| 94 | + if (! is_string($tag)) { |
| 95 | + $errors[] = "\"tags.{$i}\" must be a string."; |
95 | 96 | } |
96 | 97 | } |
97 | 98 | } |
@@ -120,37 +121,73 @@ public function store(Request $request) |
120 | 121 | $theme->user_id = auth()->id(); |
121 | 122 | $theme->save(); |
122 | 123 |
|
| 124 | + if (isset($data['tags'])) { |
| 125 | + $theme->attachTags($data['tags']); |
| 126 | + } |
| 127 | + |
123 | 128 | Cache::forget('themes:total_count'); |
124 | | - Cache::forget('themes:available_categories'); |
| 129 | + Cache::forget('themes:available_tags'); |
125 | 130 |
|
126 | 131 | return redirect()->route('themes.show', $theme->name) |
127 | 132 | ->with('success', 'Theme created successfully.'); |
128 | 133 | } |
129 | 134 |
|
130 | 135 | public function index() |
131 | 136 | { |
132 | | - $availableCategories = Cache::remember('themes:available_categories', 3600, fn () => Theme::query() |
133 | | - ->select('categories') |
134 | | - ->get() |
135 | | - ->pluck('categories') |
136 | | - ->flatten() |
137 | | - ->unique() |
138 | | - ->sort() |
139 | | - ->values() |
140 | | - ->all()); |
| 137 | + $availableTags = Cache::remember('themes:available_tags', 3600, function () { |
| 138 | + return Tag::query() |
| 139 | + ->whereExists(function ($query) { |
| 140 | + $query->select(\Illuminate\Support\Facades\DB::raw(1)) |
| 141 | + ->from('taggables') |
| 142 | + ->whereColumn('taggables.tag_id', 'tags.id') |
| 143 | + ->where('taggables.taggable_type', Theme::class); |
| 144 | + }) |
| 145 | + ->get() |
| 146 | + ->pluck('name') |
| 147 | + ->sort() |
| 148 | + ->values() |
| 149 | + ->all(); |
| 150 | + }); |
| 151 | + |
| 152 | + $query = Theme::query()->with('tags'); |
| 153 | + |
| 154 | + if ($search = request('search')) { |
| 155 | + $query->where(function ($q) use ($search) { |
| 156 | + $q->where('name', 'like', "%{$search}%") |
| 157 | + ->orWhere('title', 'like', "%{$search}%") |
| 158 | + ->orWhere('description', 'like', "%{$search}%"); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + if ($tag = request('tag')) { |
| 163 | + $query->withAnyTags([$tag]); |
| 164 | + } |
| 165 | + |
| 166 | + $themes = $query->paginate(12)->withQueryString(); |
| 167 | + |
| 168 | + $themes->getCollection()->transform(function ($theme) { |
| 169 | + $data = $theme->toArray(); |
| 170 | + $data['tags'] = $theme->tags->pluck('name')->toArray(); |
| 171 | + |
| 172 | + return $data; |
| 173 | + }); |
141 | 174 |
|
142 | 175 | return Inertia::render('themes/index', [ |
143 | | - 'themes' => Inertia::scroll(Theme::paginate(12)->withQueryString()), |
144 | | - 'filters' => request()->only(['search', 'category']), |
145 | | - 'availableCategories' => $availableCategories, |
| 176 | + 'themes' => Inertia::scroll($themes), |
| 177 | + 'filters' => request()->only(['search', 'tag']), |
| 178 | + 'availableTags' => $availableTags, |
146 | 179 | 'totalThemesCount' => Cache::remember('themes:total_count', 3600, fn () => Theme::count()), |
147 | 180 | ]); |
148 | 181 | } |
149 | 182 |
|
150 | 183 | public function show(Theme $theme) |
151 | 184 | { |
| 185 | + $theme->load('tags'); |
| 186 | + $data = $theme->toArray(); |
| 187 | + $data['tags'] = $theme->tags->pluck('name')->toArray(); |
| 188 | + |
152 | 189 | return Inertia::render('themes/show', [ |
153 | | - 'theme' => $theme, |
| 190 | + 'theme' => $data, |
154 | 191 | 'css' => $theme->toCss(), |
155 | 192 | ]); |
156 | 193 | } |
|
0 commit comments