Skip to content

Commit a7b54b8

Browse files
committed
Fix bug with SingleProduct
1 parent b9f04c5 commit a7b54b8

File tree

6 files changed

+96
-93
lines changed

6 files changed

+96
-93
lines changed

app/Http/Controllers/Api/ProductController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public function index()
1515
->get();
1616
}
1717

18-
public function show(Product $product)
18+
public function show($slug)
1919
{
20-
$product->load('categories:id,name');
20+
$product = Product::where('slug', $slug)
21+
->with(['categories:id,name'])
22+
->firstOrFail();
2123

2224
return $product;
2325
}

public/js/app.js

Lines changed: 62 additions & 45 deletions
Large diffs are not rendered by default.

resources/js/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import Navbar from "./components/Header/Navbar.vue";
1212
import MobileMenu from "./components/Header/MobileMenu.vue";
1313

1414
import ShowAllProducts from "./components/Products/ShowAllProducts.vue";
15+
import SingleProduct from "./components/Products/SingleProduct.vue";
1516

1617
import CustomerDetails from "./components/Checkout/CustomerDetails.vue";
1718
import FakeVisaDetails from "./components/Checkout/FakeVisaDetails.vue";
1819
import CartSummary from "./components/Checkout/CartSummary.vue";
1920

2021
import Carousel from "./components/Index/Carousel.vue";
2122

22-
2323
import FooterContent from "./components/Footer/Footer.vue";
2424
import OrderCheckout from "./components/Checkout/OrderCheckout.vue";
2525
import OrderForm from "./components/Checkout/OrderForm.vue";
@@ -50,6 +50,7 @@ app.component("layout-component", Layout);
5050
app.component("showall-products", ShowAllProducts);
5151
app.component("order-checkout", OrderCheckout);
5252
app.component("order-form", OrderForm);
53+
app.component("single-product", SingleProduct);
5354

5455
app.use(pinia);
5556
app.use(router);

resources/js/components/Products/ShowAllProducts.vue

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
<template>
22
<div v-if="data && data.length">
33
<div id="product-container" class="flex flex-wrap items-center">
4-
<div
5-
v-for="product in data"
6-
:key="product.id"
7-
class="flex flex-col mt-6 md:pr-6 xl:pr-6 sm:w1/2 md:w-1/3 lg:1/4 xl:w-1/4"
8-
>
9-
<router-link
10-
:to="{
11-
name: 'single.product',
12-
params: { slug: product.slug, product: JSON.stringify(product) },
13-
}"
14-
>
15-
<img
16-
v-if="product.imageUrl"
17-
class="productImage"
18-
:alt="product.name"
19-
:src="product.imageUrl"
20-
/>
4+
<div v-for="product in data" :key="product.id"
5+
class="flex flex-col mt-6 md:pr-6 xl:pr-6 sm:w1/2 md:w-1/3 lg:1/4 xl:w-1/4">
6+
<router-link :to="{
7+
name: 'single.product',
8+
params: { slug: product.slug },
9+
}">
10+
<img v-if="product.imageUrl" class="productImage" :alt="product.name" :src="product.imageUrl" />
2111
<div class="flex justify-center pt-3">
2212
<p class="text-xl font-bold text-center cursor-pointer">
2313
{{ product.name }}
2414
</p>
2515
</div>
2616
<div class="flex justify-center mt-2">
27-
<div class="text-xl text-gray-900">kr {{ product.price }}</div>
17+
<div class="text-xl text-gray-900">
18+
kr {{ product.price }}
19+
</div>
2820
</div>
2921
<div class="flex justify-center pt-3">
3022
<base-button backgroundColor="bg-blue-600">View Product</base-button>
@@ -34,7 +26,9 @@
3426
</div>
3527
</div>
3628
<div v-if="error">
37-
<h1 class="text-center text-3xl text-red-500 m-4 p-4 mt-8">Error loading products</h1>
29+
<h1 class="text-center text-3xl text-red-500 m-4 p-4 mt-8">
30+
Error loading products
31+
</h1>
3832
</div>
3933
</template>
4034

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
11
<template>
2-
<div v-if="product">
2+
<div v-if="data">
33
<section>
44
<div class="container z-0 flex flex-wrap items-center pt-4 pb-12 mx-auto">
5-
<div
6-
class="grid grid-cols-1 gap-4 mt-8 lg:grid-cols-2 xl:grid-cols-2 md:grid-cols-2 sm:grid-cols-2"
7-
>
8-
<img
9-
v-if="product.imageUrl"
10-
id="product-image"
11-
class="productImage"
12-
:alt="product.name"
13-
:src="product.imageUrl"
14-
/>
15-
<img
16-
v-else
17-
id="product-image"
5+
<div class="grid grid-cols-1 gap-4 mt-8 lg:grid-cols-2 xl:grid-cols-2 md:grid-cols-2 sm:grid-cols-2">
6+
<img v-if="data.imageUrl" id="product-image" class="productImage" :alt="data.name" :src="data.imageUrl" />
7+
<img v-else id="product-image"
188
class="h-auto p-8 transition duration-500 ease-in-out transform xl:p-2 md:p-2 lg:p-2 hover:shadow-lg hover:scale-105"
19-
:alt="product.name"
20-
:src="placeholderImage"
21-
/>
9+
:alt="data.name" :src="placeholderImage" />
2210
<div class="ml-8">
2311
<p class="text-3xl font-bold text-left">
24-
{{ product.name }}
12+
{{ data.name }}
2513
</p>
2614
<p class="pt-1 mt-4 text-2xl text-gray-900">
27-
{{ formatPrice(product.price) }}
15+
{{ formatPrice(data.price) }}
2816
</p>
2917
<p class="pt-1 mt-4 text-2xl text-gray-900">
30-
{{ product.description }}
18+
{{ data.description }}
3119
</p>
3220
<div class="pt-1 mt-2">
33-
<base-button
34-
backgroundColor="bg-blue-600"
35-
@click="store.addToCart({ item: product })"
36-
>Add To Cart</base-button
37-
>
21+
<base-button backgroundColor="bg-blue-600" @click="store.addToCart({ item: data })">Add To
22+
Cart</base-button>
3823
</div>
3924
</div>
4025
</div>
@@ -44,6 +29,8 @@
4429
</template>
4530

4631
<script setup>
32+
import useSWRV from "swrv";
33+
4734
import { useRoute } from "vue-router";
4835
4936
import { useCart } from "../../store/useCart";
@@ -52,5 +39,6 @@ import { formatPrice } from "../../utils/functions";
5239
const route = useRoute();
5340
const store = useCart();
5441
55-
const product = JSON.parse(route.params.product);
42+
const fetcher = (key) => fetch(key).then((res) => res.json());
43+
const { data, error } = useSWRV(`/api/product/${route.params.slug}`, fetcher);
5644
</script>

resources/js/router/routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const routes = [
1515
name: "single.product",
1616
path: "/product/:slug",
1717
component: SingleProduct,
18+
props: true
1819
},
1920
{ path: "/checkout", component: Checkout },
2021
{ path: "/categories", component: Categories },

0 commit comments

Comments
 (0)