2021-11-19 08:04:04 +01:00
|
|
|
<?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;
|
2021-12-08 16:39:56 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2021-11-19 08:04:04 +01:00
|
|
|
|
|
|
|
class CartController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Models\Product $product
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show()
|
|
|
|
{
|
2021-12-08 16:39:56 +01:00
|
|
|
$user = Auth::user();
|
|
|
|
if (!$user) {
|
|
|
|
throw new AccessDeniedHttpException("Not logged in");
|
|
|
|
}
|
|
|
|
return view("cart", ["user" => $user]);
|
2021-11-19 08:04:04 +01:00
|
|
|
}
|
|
|
|
|
2021-11-24 11:34:46 +01:00
|
|
|
public function addToCart(Product $product)
|
|
|
|
{
|
2021-11-19 08:04:04 +01:00
|
|
|
$user = Auth::user();
|
|
|
|
$user->cart()->syncWithoutDetaching([$product->uuid]);
|
|
|
|
|
|
|
|
return back();
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:34:46 +01:00
|
|
|
public function removeFromCart(Product $product)
|
|
|
|
{
|
2021-11-19 08:04:04 +01:00
|
|
|
$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");
|
|
|
|
}
|
|
|
|
}
|