Files
jouwdriedee/app/Http/Controllers/Admin/ProductController.php
T

161 lines
5.4 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\View\View;
class ProductController extends Controller
{
/**
* Display a listing of products.
*/
public function index(Request $request): View
{
$tab = $request->query('tab', 'all');
$query = Product::with(['category', 'makerworldCollection'])->latest();
if ($tab === 'published') {
$query->where(function ($q) {
$q->where('status', 'published')
->orWhereNull('status');
});
} elseif ($tab === 'draft') {
$query->where('status', 'draft');
} elseif ($tab === 'makerworld') {
$query->whereNotNull('makerworld_id');
}
if ($request->filled('q')) {
$q = $request->input('q');
$query->where(function ($w) use ($q) {
$w->where('name', 'like', "%{$q}%")
->orWhere('description', 'like', "%{$q}%")
->orWhere('makerworld_id', 'like', "%{$q}%");
});
}
if ($request->filled('category_id')) {
$query->where('category_id', $request->input('category_id'));
}
$totalAll = Product::count();
$totalPublished = Product::where('status', 'published')->orWhereNull('status')->count();
$totalDraft = Product::where('status', 'draft')->count();
$totalMakerWorld = Product::whereNotNull('makerworld_id')->count();
$products = $query->paginate(20)->withQueryString();
$categories = Category::all();
return view('admin.products.index', compact(
'products',
'categories',
'tab',
'totalAll',
'totalPublished',
'totalDraft',
'totalMakerWorld'
));
}
/**
* Show the form for creating a new product.
*/
public function create(): View
{
$categories = Category::all();
return view('admin.products.create', compact('categories'));
}
/**
* Store a newly created product in storage.
*/
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'category_id' => 'required|exists:categories,id',
'base_price' => 'required|numeric|min:0.01',
'short_description' => 'nullable|string|max:500',
'description' => 'required|string',
'image_url' => 'nullable|string|max:500',
'dimensions' => 'nullable|string|max:100',
'weight_grams' => 'nullable|integer|min:0',
'print_time_hours' => 'nullable|numeric|min:0',
'is_featured' => 'nullable|boolean',
'is_active' => 'nullable|boolean',
]);
$validated['slug'] = Str::slug($validated['name']);
$validated['is_featured'] = $request->boolean('is_featured');
$validated['is_active'] = $request->boolean('is_active');
$validated['available_materials'] = ['pla', 'petg', 'silk', 'tpu'];
$validated['available_colors'] = [
'Zon Geel (Pikachu)', 'Vurig Rood (Charizard)', 'Hemelsblauw (Squirtle)',
'Muntgroen (Bulbasaur)', 'Spooky Paars (Gengar)', 'Koper Zijdeglans',
'Regenboog Multi-Color', 'Mat Zwart', 'Sneeuwwit',
];
Product::create($validated);
return redirect()->route('admin.products.index')
->with('success', "Product '{$validated['name']}' is succesvol aangemaakt!");
}
/**
* Show the form for editing the specified product.
*/
public function edit(Product $product): View
{
$categories = Category::all();
return view('admin.products.edit', compact('product', 'categories'));
}
/**
* Update the specified product in storage.
*/
public function update(Request $request, Product $product): RedirectResponse
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'category_id' => 'required|exists:categories,id',
'base_price' => 'required|numeric|min:0.01',
'short_description' => 'nullable|string|max:500',
'description' => 'required|string',
'image_url' => 'nullable|string|max:500',
'dimensions' => 'nullable|string|max:100',
'weight_grams' => 'nullable|integer|min:0',
'print_time_hours' => 'nullable|numeric|min:0',
'is_featured' => 'nullable|boolean',
'is_active' => 'nullable|boolean',
]);
$validated['is_featured'] = $request->boolean('is_featured');
$validated['is_active'] = $request->boolean('is_active');
$product->update($validated);
return redirect()->route('admin.products.index')
->with('success', "Product '{$product->name}' is succesvol bijgewerkt!");
}
/**
* Remove the specified product from storage.
*/
public function destroy(Product $product): RedirectResponse
{
$name = $product->name;
$product->delete();
return redirect()->route('admin.products.index')
->with('success', "Product '{$name}' is verwijderd.");
}
}