diff --git a/app/Modules/Inventory/Controllers/ProductController.php b/app/Modules/Inventory/Controllers/ProductController.php index cbd349b..ec0a41c 100644 --- a/app/Modules/Inventory/Controllers/ProductController.php +++ b/app/Modules/Inventory/Controllers/ProductController.php @@ -260,6 +260,10 @@ class ProductController extends Controller $product->update($validated); + if ($request->input('from') === 'show') { + return redirect()->route('products.show', $product->id)->with('success', '商品已更新'); + } + return redirect()->route('products.index')->with('success', '商品已更新'); } diff --git a/resources/js/Components/Product/ProductForm.tsx b/resources/js/Components/Product/ProductForm.tsx index 1a7779c..a30cef4 100644 --- a/resources/js/Components/Product/ProductForm.tsx +++ b/resources/js/Components/Product/ProductForm.tsx @@ -22,6 +22,8 @@ export default function ProductForm({ units, }: ProductFormProps) { const isEdit = !!initialData; + const urlParams = new URLSearchParams(window.location.search); + const from = urlParams.get('from'); const { data, setData, post, put, processing, errors } = useForm({ code: initialData?.code || "", @@ -45,7 +47,7 @@ export default function ProductForm({ e.preventDefault(); if (isEdit) { - put(route("products.update", initialData.id), { + put(route("products.update", { product: initialData.id, from }), { onSuccess: () => toast.success("商品已更新"), onError: () => toast.error("更新失敗,請檢查輸入資料"), }); diff --git a/resources/js/Pages/Product/Edit.tsx b/resources/js/Pages/Product/Edit.tsx index cdc3eed..ca64520 100644 --- a/resources/js/Pages/Product/Edit.tsx +++ b/resources/js/Pages/Product/Edit.tsx @@ -14,6 +14,11 @@ interface Props { } export default function Edit({ product, categories, units }: Props) { + const urlParams = new URLSearchParams(window.location.search); + const from = urlParams.get('from'); + const backUrl = from === 'show' ? route('products.show', product.id) : route('products.index'); + const backText = from === 'show' ? "返回商品詳情" : "返回商品列表"; + return ( {/* Header */}
- + diff --git a/resources/js/Pages/Product/Show.tsx b/resources/js/Pages/Product/Show.tsx index 6f6272c..f7cd1bf 100644 --- a/resources/js/Pages/Product/Show.tsx +++ b/resources/js/Pages/Product/Show.tsx @@ -67,7 +67,7 @@ export default function ProductShow({ product }: Props) {
- +