Add product quantities

This commit is contained in:
bad 2021-12-14 18:03:22 +01:00
parent 6b34c59d73
commit 9fcdd131c8
9 changed files with 83 additions and 19 deletions

1
.envrc
View file

@ -1 +1,2 @@
use nix
layout php

View file

@ -27,10 +27,19 @@ class CartController extends Controller
return view("cart", ["user" => $user]);
}
public function addToCart(Product $product)
public function addToCart(Product $product, Request $request)
{
$user = Auth::user();
$user->cart()->syncWithoutDetaching([$product->uuid]);
$quantity = $request->validate([
'quantity' => 'numeric|min:1'
])["quantity"];
if($quantity) {
$pivot = $user->cart()->findOrFail($product->uuid)->pivot;
$pivot->quantity = $quantity;
$pivot->save();
}
return back();
}

View file

@ -37,10 +37,12 @@ class OrderController extends Controller
{
$validated = $request->validate([
'products' => 'required|array',
'products.*' => 'exists:products,uuid'
'products.*' => 'exists:products,uuid',
'product_quantities' => 'required|array',
'product_quantities.*' => 'numeric|min:1',
]);
$products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]);
return view("order/create", ["products" => $products]);
$products_quantities = array_map(fn ($v, $q) => [Product::query()->where("uuid", $v)->first(), $q], $validated["products"], $validated["product_quantities"]);
return view("order/create", ["products_with_quantities" => $products_quantities]);
//
}
@ -60,11 +62,13 @@ class OrderController extends Controller
$validated = $request->validate([
'products' => 'required|array',
'products.*' => 'exists:products,uuid',
'product_quantities' => 'required|array',
'product_quantities.*' => 'numeric|min:1',
'address' => 'required',
]);
$products_quantities = array_map(fn ($v, $q) => [Product::query()->where("uuid", $v)->first(), $q], $validated["products"], $validated["product_quantities"]);
$products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]);
DB::transaction(function () use ($validated, $user, $products) {
DB::transaction(function () use ($validated, $user, $products_quantities) {
$address = new Address($validated);
$address->user()->associate($user);
$address->save();
@ -73,16 +77,20 @@ class OrderController extends Controller
$order->address()->associate($address);
$order->user()->associate($user);
$order->cost = array_reduce($products, fn ($c, $i) => bcadd($c,$i->price), 0);
$order->cost = array_reduce($products_quantities, fn ($c, $i) => bcadd($c,bcmul($i[0]->price,$i[1])), 0);
$order->save();
foreach($products as $product) {
foreach($products_quantities as $product_quantity) {
$product = $product_quantity[0];
$quantity = $product_quantity[1];
$order->products()->attach($product);
$order->products()->find($product->uuid)->pivot->quantity = $quantity;
}
});
foreach($products as $product) {
$user->cart()->detach($product);
foreach($products_quantities as $product) {
$user->cart()->detach($product[0]);
}
return redirect()->route("order.index");
}
/**

View file

@ -23,6 +23,6 @@ class Order extends Model
public function products()
{
return $this->belongsToMany(Product::class, "orderProduct", "orderId" ,"productId");
return $this->belongsToMany(Product::class, "orderProduct", "orderId" ,"productId")->withPivot("quantity");
}
}

View file

@ -52,7 +52,7 @@ class User extends Authenticatable
public function cart()
{
return $this->belongsToMany(Product::class, "cart_items", "userID", "productID");
return $this->belongsToMany(Product::class, "cart_items", "userID", "productID")->withPivot("quantity");
}
public function orders()

View file

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddCartItemQuantity extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('cart_items', function (Blueprint $table) {
$table->unsignedInteger("quantity")->default(1);
});
Schema::table('orderProduct', function (Blueprint $table) {
$table->unsignedInteger("quantity")->default(1);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('cart_items', function (Blueprint $table) {
$table->dropColumn("quantity");
});
Schema::table('orderProduct', function (Blueprint $table) {
$table->dropColumn("quantity");
});
}
}

View file

@ -12,9 +12,17 @@
<div>
<div class="cart-items">
@foreach($user->cart as $product)
{{-- dd($product->pivot->quantity) --}}
<div class="cart-item">
<x-product :product="$product" />
<form method="POST" action="{{route("addToCart",$product)}}">
<input type="number" value="{{ $product->pivot->quantity }}" name="quantity">
<button type="submit"> Zatwierdź </button>
@csrf
</form>
<form class="remove-cart-form" action="{{ route("removeFromCart", $product) }}" method="post">
@csrf
<button type="submit"> Remove from cart </button>
</form>
@ -26,6 +34,7 @@
@csrf
@foreach($user->cart as $product)
<input type="hidden" name="products[{{ $loop->index }}]" value="{{ $product->uuid }}">
<input type="hidden" name="product_quantities[{{ $loop->index }}]" value="{{ $product->pivot->quantity }}">
@endforeach()
<button type="submit"> Order </button>
</form>

View file

@ -15,9 +15,10 @@
@endif
<div>
<form method="post" action="{{ route("order.store") }}">
@foreach($products as $product)
<input type="hidden" name="products[{{ $loop->index }}]" value="{{ $product->uuid }}">
<x-product :product="$product" />
@foreach($products_with_quantities as $product)
<input type="hidden" name="products[{{ $loop->index }}]" value="{{ $product[0]->uuid }}">
<x-product :product="$product[0]"/>
<input type="number" name="product_quantities[{{ $loop->index }}]" value="{{ $product[1] }}">
@endforeach
<textarea name="address"></textarea>

View file

@ -9,8 +9,6 @@
@section('main')
<p>
<b>Welcome, you are logged in as {{ $user->name }}</b>
</p>
<p>
<a href="/logout"> Logout </a>
<a href="{{route("logout")}}"> Logout </a>
</p>
@endsection()