/
hikqri
/
up
Обзор
Документация
Войти
/
hikqri
/
up
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/Http/Controllers/CheckoutController.php
90 строк
2 KB
Myckaa
first_commit
26 июн 2026, 18:36
26 июн 2026, 18:36
8095503
Код
Авторство
О чём код?
<?php namespace App\Http\Controllers; use App\Models\Order; use App\Models\OrderItem; use App\Services\CartService; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class CheckoutController extends Controller { public function __construct(protected CartService $cart) { } public function index() { if ($this->cart->isEmpty()) { return redirect()->route('cart.index'); } $user = auth()->user(); return view('checkout.index', [ 'items' => $this->cart->items(), 'total' => $this->cart->total(), 'user' => $user, ]); } public function store(Request $request) { if ($this->cart->isEmpty()) { return redirect()->route('cart.index'); } $data = $request->validate([ 'customer_name' => ['required', 'string', 'max:200'], 'customer_email' => ['required', 'email'], 'customer_phone' => ['required', 'string', 'max:20'], 'address' => ['required', 'string', 'max:500'], 'comment' => ['nullable', 'string', 'max:1000'], ]); $items = $this->cart->items(); $total = $this->cart->total(); DB::transaction(function () use ($data, $items, $total) { $order = Order::create([ 'user_id' => auth()->id(), 'status' => 'pending', 'total' => $total, 'customer_name' => $data['customer_name'], 'customer_email' => $data['customer_email'], 'customer_phone' => $data['customer_phone'], 'address' => $data['address'], 'comment' => $data['comment'] ?? null, ]); foreach ($items as $item) { OrderItem::create([ 'order_id' => $order->id, 'product_id' => $item->product->id, 'quantity' => $item->quantity, 'price' => $item->product->price, ]); } session(['last_order_id' => $order->id]); }); $this->cart->clear(); return redirect()->route('checkout.success'); } public function success() { $orderId = session('last_order_id'); if (!$orderId) { return redirect('/'); } $order = Order::with('items.product')->find($orderId); return view('checkout.success', compact('order')); } }