iT邦幫忙

2026 iThome 鐵人賽

DAY 14
0
自我挑戰組

Laravel 讀碼源系列 第 14

產品管理列表的修改前端code

  • 分享至 

  • xImage
  •  

修改的部分只能截圖
https://ithelp.ithome.com.tw/upload/images/20260823/20119035bT9p6atrFS.png

https://ithelp.ithome.com.tw/upload/images/20260823/20119035uuBZ0GchRh.png

https://ithelp.ithome.com.tw/upload/images/20260823/20119035q8yA1x15vU.png

https://ithelp.ithome.com.tw/upload/images/20260823/20119035c7sWxksKNu.png

https://ithelp.ithome.com.tw/upload/images/20260823/20119035tx4qF5vzwV.png

https://ithelp.ithome.com.tw/upload/images/20260823/20119035DY2UzUREN2.png

https://ithelp.ithome.com.tw/upload/images/20260823/201190357QJh1Evw3b.png

<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>編輯產品 - 我的商店</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft JhengHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 40px 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
        }

        .back-btn {
            display: inline-block;
            background: white;
            color: #667eea;
            padding: 12px 30px;
            border-radius: 25px;
            text-decoration: none;
            font-weight: 600;
            margin-bottom: 20px;
            transition: all 0.3s;
            border: 2px solid white;
        }

        .back-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 255, 255, 0.3);
        }

        .form-card {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }

        .form-header {
            border-bottom: 3px solid #667eea;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }

        .form-header h2 {
            color: #667eea;
            font-size: 2em;
            font-weight: 700;
        }

        .product-id-badge {
            display: inline-block;
            background: #e3f2fd;
            color: #1976d2;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 0.9em;
            font-weight: 600;
            margin-top: 10px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 8px;
            font-size: 1.05em;
        }

        .required {
            color: #e74c3c;
        }

        .form-control {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #ecf0f1;
            border-radius: 10px;
            font-size: 1em;
            font-family: inherit;
            transition: all 0.3s;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        textarea.form-control {
            min-height: 120px;
            resize: vertical;
        }

        .help-text {
            color: #95a5a6;
            font-size: 0.9em;
            margin-top: 5px;
        }

        .error-text {
            color: #e74c3c;
            font-size: 0.9em;
            margin-top: 5px;
        }

        .images-section {
            margin-bottom: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 10px;
        }

        .images-section h3 {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        .images-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
            gap: 15px;
        }

        .image-item {
            position: relative;
            border-radius: 10px;
            overflow: hidden;
            aspect-ratio: 1;
            background: white;
            border: 2px solid #ecf0f1;
        }

        .image-item img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .delete-image {
            position: absolute;
            top: 5px;
            right: 5px;
            background: rgba(231, 76, 60, 0.9);
            color: white;
            border: none;
            width: 28px;
            height: 28px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s;
        }

        .delete-image:hover {
            background: #c0392b;
            transform: scale(1.1);
        }

        .no-images {
            text-align: center;
            padding: 40px;
            color: #95a5a6;
        }

        .btn-submit {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 1.2em;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
        }

        .alert {
            padding: 15px 20px;
            border-radius: 10px;
            margin-bottom: 20px;
        }

        .alert-danger {
            background: #fee;
            border: 1px solid #fcc;
            color: #c33;
        }

        .alert-success {
            background: #efe;
            border: 1px solid #cfc;
            color: #3c3;
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="{{ route('admin.products.card') }}" class="back-btn">← 返回產品列表</a>

        <div class="form-card">
            <div class="form-header">
                <h2>✏️ 編輯產品</h2>
                <span class="product-id-badge">#{{ $product->id }}</span>
            </div>

            @if ($errors->any())
                <div class="alert alert-danger">
                    <strong>請修正以下錯誤:</strong>
                    <ul style="margin: 10px 0 0 20px;">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            @if (session('success'))
                <div class="alert alert-success">
                    {{ session('success') }}
                </div>
            @endif

            <!-- 產品圖片 -->
            <div class="images-section">
                <h3>📷 產品圖片</h3>
                @if($product->images && product->images->count()>0)<divclass="images-grid">@foreach(product->images as $image)
                            <div class="image-item">
                                <img src="/show-image.php?path={{ urlencode($image->path) }}" alt="產品圖片">
                                <button class="delete-image" onclick="deleteImage({{ $image->id }})">
                                    ×
                                </button>
                            </div>
                        @endforeach
                    </div>
                @else
                    <div class="no-images">
                        <p>📭 尚無圖片</p>
                    </div>
                @endif
            </div>

            <!-- 編輯表單 -->
            <form method="POST" action="{{ route('admin.products.update', $product->id) }}">
                @csrf
                @method('PUT')

                <div class="form-group">
                    <label>產品標題 <span class="required">*</span></label>
                    <input type="text" name="title" class="form-control"
                           value="{{ old('title', $product->title) }}"
                           required>
                    @error('title')
                        <p class="error-text">{{ $message }}</p>
                    @enderror
                </div>

                <div class="form-group">
                    <label>產品說明</label>
                    <textarea name="content" class="form-control">{{ old('content', $product->content) }}</textarea>
                    @error('content')
                        <p class="error-text">{{ $message }}</p>
                    @enderror
                    <p class="help-text">詳細描述產品特色與規格</p>
                </div>

                <div class="form-group">
                    <label>價格 <span class="required">*</span></label>
                    <input type="number" name="price" class="form-control"
                           value="{{ old('price', $product->price) }}"
                           min="0" step="1" required>
                    @error('price')
                        <p class="error-text">{{ $message }}</p>
                    @enderror
                    <p class="help-text">請輸入整數金額(新台幣)</p>
                </div>

                <div class="form-group">
                    <label>庫存數量 <span class="required">*</span></label>
                    <input type="number" name="quantity" class="form-control"
                           value="{{ old('quantity', $product->quantity) }}"
                           min="0" required>
                    @error('quantity')
                        <p class="error-text">{{ $message }}</p>
                    @enderror
                </div>

                <button type="submit" class="btn-submit">
                    💾 儲存變更
                </button>
            </form>
        </div>
    </div>

    <script>
    function deleteImage(imageId) {
        if (!confirm('確定要刪除這張圖片嗎?')) {
            return;
        }

        $.ajax({
            url: '/admin/products/images/' + imageId,
            method: 'DELETE',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            success: function(response) {
                location.reload();
            },
            error: function(xhr) {
                alert('刪除失敗,請稍後再試');
            }
        });
    }
    </script>
</body>
</html>


明天再來解釋CODE~


上一篇
解釋產品管理列表的後端code
下一篇
解釋產品管理列表的修改前端code
系列文
Laravel 讀碼源22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言