withCount('orders'); if ($request->filled('q')) { $query->where(function ($q) use ($request) { $q->where('name', 'like', '%'.$request->q.'%') ->orWhere('email', 'like', '%'.$request->q.'%'); }); } $customers = $query->latest()->paginate(25); return view('admin.customers.index', compact('customers')); } public function show(User $customer) { abort_if($customer->isAdmin(), 404); $customer->load('addresses'); $orders = $customer->orders() ->with('addresses') ->latest() ->paginate(15); $stats = [ 'total_orders' => $customer->orders()->count(), 'total_revenue' => $customer->orders() ->whereNotIn('status', ['cancelled']) ->sum('total'), 'open_orders' => $customer->orders() ->whereIn('status', ['new', 'payment_received', 'processing']) ->count(), 'last_order' => $customer->orders()->latest()->first(), ]; return view('admin.customers.show', compact('customer', 'orders', 'stats')); } public function toggleApproval(User $customer) { abort_if($customer->isAdmin(), 403); $approving = ! $customer->approved; $customer->approved = $approving; if ($approving && ! $customer->email_verified_at) { $customer->email_verified_at = now(); } $customer->save(); $status = $approving ? 'freigeschaltet' : 'gesperrt'; return back()->with('success', "Kunde {$customer->name} wurde {$status}."); } public function destroy(User $customer) { abort_if($customer->isAdmin(), 403); $customer->delete(); return redirect()->route('admin.customers.index')->with('success', 'Kunde gelöscht.'); } }