This commit is contained in:
bad 2021-11-19 08:04:04 +01:00
parent 5c231d2039
commit f725731a6a
6 changed files with 121 additions and 0 deletions

View file

@ -0,0 +1,64 @@
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\User;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class CartController extends Controller
{
/**
* Display the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show()
{
throw new Exception("UNIMPLEMENTED");
//return view("product/view", ["product" => $product]);
}
public function addToCart(Product $product) {
$user = Auth::user();
$user->cart()->syncWithoutDetaching([$product->uuid]);
return back();
}
public function removeFromCart(Product $product) {
$user = Auth::user();
$user->cart()->detach($product);
return back();
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
$product->fill($request->all());
$product->save();
return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully");
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->route("product.index");
}
}

View file

@ -44,4 +44,8 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];
public function cart() {
return $this->belongsToMany(Product::class, "cart_items", "userID", "productID");
}
}

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCartItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cart_items', function (Blueprint $table) {
$table->id();
$table->foreignUuid("userID")->references("uuid")->on("users");
$table->foreignUuid("productID")->references("uuid")->on("products");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cart_items');
}
}

View file

@ -17,6 +17,15 @@
@method('DELETE')
@csrf
</form>
<form method="POST" action="{{ route("addToCart", $product) }}">
<button type="submit">Add to cart</button>
@csrf
</form>
</form>
<form method="POST" action="{{ route("removeFromCart", $product) }}">
<button type="submit">Remove from cart</button>
@csrf
</form>
</div>
</body>
</html>

View file

@ -9,6 +9,14 @@
<p>
<b>Welcome, you are logged in as {{ $user }}</b>
</p>
<div>
<h2>Your cart:</h2>
<ul>
@foreach($user->cart as $product)
<li> {{ $product }} </li>
@endforeach()
</ul>
</div>
<p>
<a href="/logout"> Logout </a>
</p>

View file

@ -3,6 +3,7 @@
use App\Http\Controllers\ImageController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\MainPageController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Auth;
@ -32,6 +33,9 @@ Route::post('/login', [LoginController::class, "authenticate"])->name("login");
Route::post('/register', [LoginController::class, "register"])->name("register");
Route::any("/logout", [LoginController::class, "logout"])->name("logout");
Route::post("/product/{product}/addToCart", [CartController::class, "addToCart"])->name("addToCart");
Route::post("/product/{product}/removeFromCart", [CartController::class, "removeFromCart"])->name("removeFromCart");
Route::resource("product", ProductController::class);
Route::resource("order", OrderController::class);
Route::resource("image", ImageController::class)->only(["store", "delete", "create"]);