# Store ID Storage & Display Fix Guide

## Issue Summary
The `store_id` column is not being stored in the `carts` and `orders` tables, and store names are not displaying on the cart and order pages. This is likely because **database migrations haven't been run**.

---

## Solution: Run Database Migrations

### Step 1: Run Pending Migrations
Open PowerShell in your project root and run:

```powershell
cd 'C:\wamp64\www\onlineshop'
php artisan migrate
```

This will create:
1. `stores` table (from migration `2025_11_26_000001_create_stores_table.php`)
2. Add `store_id` columns to `products`, `orders`, and `carts` tables (from migration `2025_11_26_000002_add_store_id_to_products_orders_carts.php`)

### Step 2: Create Sample Store Data
After migration, create at least one store:

```powershell
php artisan tinker
>>> \App\Store::create(['name'=>'Main Store','code'=>'MAIN','address'=>'123 Main St','phone'=>'555-0100']);
>>> \App\Store::create(['name'=>'Branch Store','code'=>'BRANCH','address'=>'456 Branch Ave','phone'=>'555-0101']);
>>> exit()
```

---

## Verification Checklist

After running migrations, verify:

- [ ] **Database columns created:**
  - `stores.id`, `stores.name`, `stores.code`, `stores.address`, `stores.phone`, `stores.created_at`, `stores.updated_at`
  - `products.store_id` (nullable, foreign key)
  - `orders.store_id` (nullable, foreign key)
  - `carts.store_id` (nullable, foreign key)

- [ ] **Stores table has data:**
  - Use `php artisan tinker` → `\App\Store::count()` → should return `>= 1`

- [ ] **Admin Product Create/Edit Form:**
  - Go to Admin → Products → Create/Edit
  - Confirm "Store" select dropdown appears
  - Create/Edit a product and select a store

- [ ] **Frontend Cart Shows Store:**
  - Add that product to cart
  - Go to View Cart page
  - Confirm store name appears above the product items
  - Check database: `SELECT * FROM carts WHERE product_id = <id>;` → `store_id` should NOT be NULL

- [ ] **Checkout & Order Shows Store:**
  - Complete checkout
  - Order confirmation page should show store name
  - Order details page should display store

---

## Code Changes Made

### 1. **CartController.php** (Line ~173)
```php
// Assigns product's store_id to cart item
$data['store_id'] = $product->store_id;
```

✅ Already implemented.

### 2. **Cart Model** (`app/Cart.php`)
```php
protected $fillable = [..., 'store_id'];

public function store()
{
    return $this->belongsTo(Store::class);
}
```

✅ Already implemented.

### 3. **Frontend Cart Views** (Updated)

**`resources/views/frontend/view_cart.blade.php`** - Groups items by store and shows store header:
```blade
@foreach ($cartsByStore as $storeId => $storeItems)
    @if($store)
        <li class="list-group-item px-0 px-lg-3 py-3 bg-light">
            <strong class="text-dark">{{ translate('Store') }}: {{ $store->name }}</strong>
        </li>
    @endif
    @foreach ($storeItems as $key => $cartItem)
        <!-- Cart item display -->
    @endforeach
@endforeach
```

**`resources/views/frontend/partials/cart_details.blade.php`** - Same grouping logic.

✅ Already implemented.

### 4. **Order Details View** (`resources/views/frontend/user/order_details_customer.blade.php`)
```blade
<tr>
    <td class="w-50 fw-600">{{ translate('Store')}}:</td>
    <td>{{ $order->store ? $order->store->name : translate('Main Store') }}</td>
</tr>
```

✅ Already implemented.

### 5. **OrderController.php** - Eager Loading
```php
$orders = Order::with('store')->orderBy('id', 'desc');
```

✅ Already implemented.

---

## If Store ID Still Doesn't Save After Migration

**Debug Steps:**

1. **Check if Product has store_id set:**
   ```powershell
   php artisan tinker
   >>> $p = \App\Product::find(1);
   >>> $p->store_id;  # Should NOT be NULL if you set it in admin form
   ```

2. **Check Cart data:**
   ```powershell
   >>> $c = \App\Cart::first();
   >>> $c->store_id;  # Should NOT be NULL after adding a product
   ```

3. **Verify Product fillable includes store_id:**
   - Open `app/Product.php`
   - Check `$fillable` includes `'store_id'` ✅ (confirmed)

4. **Verify ProductController saves store_id:**
   - Open `app/Http/Controllers/ProductController.php`
   - Check create/update methods set `$product->store_id = $request->store_id;`
   - ✅ (confirmed in previous edits)

---

## Files Modified/Created

| File | Status | Purpose |
|------|--------|---------|
| `database/migrations/2025_11_26_000001_create_stores_table.php` | ✅ Created | Creates stores table |
| `database/migrations/2025_11_26_000002_add_store_id_to_products_orders_carts.php` | ✅ Created | Adds store_id FK to 3 tables |
| `app/Store.php` | ✅ Created | Store model with relationships |
| `app/Product.php` | ✅ Updated | Added store_id to fillable, store() method |
| `app/Cart.php` | ✅ Updated | Added store_id to fillable, store() method |
| `app/Order.php` | ✅ Updated | Added store() relationship method |
| `app/Http/Controllers/ProductController.php` | ✅ Updated | Passes $stores to create/edit views, saves store_id |
| `app/Http/Controllers/CartController.php` | ✅ Updated | Assigns product->store_id to cart item |
| `app/Http/Controllers/OrderController.php` | ✅ Updated | Sets order->store_id from first cart item, eager loads store |
| `resources/views/frontend/view_cart.blade.php` | ✅ Updated | Groups cart by store, shows store name |
| `resources/views/frontend/partials/cart_details.blade.php` | ✅ Updated | Groups cart by store, shows store name |
| `resources/views/frontend/user/order_details_customer.blade.php` | ✅ Updated | Shows store name in order summary |
| `resources/views/backend/product/products/create.blade.php` | ✅ Updated | Added store select dropdown |
| `resources/views/backend/product/products/edit.blade.php` | ✅ Updated | Added store select dropdown |
| `app/Http/Controllers/ReportController.php` | ✅ Updated | Added store filters to reports + new store_sale_report |
| `resources/views/backend/reports/store_sale_report.blade.php` | ✅ Created | Store-based sales aggregation report |
| `routes/admin.php` | ✅ Updated | Added store_sale_report route |

---

## Quick Test Workflow

1. **Run migrations:**
   ```powershell
   php artisan migrate
   ```

2. **Create stores:**
   ```powershell
   php artisan tinker
   >>> \App\Store::create(['name'=>'Store 1','address'=>'Address 1','phone'=>'555-0001']);
   >>> \App\Store::create(['name'=>'Store 2','address'=>'Address 2','phone'=>'555-0002']);
   >>> exit()
   ```

3. **Admin: Create/Edit product with store:**
   - Navigate to Admin → Products → Create
   - Fill form and select a store from the dropdown
   - Save product

4. **Frontend: Add to cart and verify:**
   - Browse to product page
   - Add product to cart
   - Go to View Cart page
   - Verify store name displays above product
   - Verify database: `SELECT store_id FROM carts WHERE id = <cart_id>;` is NOT NULL

5. **Checkout & Order confirmation:**
   - Complete checkout
   - Verify order shows store name
   - Verify order details page shows store

---

## Troubleshooting

| Problem | Solution |
|---------|----------|
| "store_id column doesn't exist" | Run `php artisan migrate` |
| Store dropdown doesn't appear in product form | Check `ProductController` passes `$stores` to create/edit views |
| Store name shows "Main Store" even after setting | Product may not have store_id saved; edit product and set store again |
| Cart groups don't show store | Check `carts.store_id` is NULL in database; re-add product to cart |
| Order doesn't show store | Check `orders.store_id` is NULL; complete new checkout |
| Static analyzer warnings about undefined types | These are lint warnings and don't block runtime; can be ignored or fixed by adding `use` statements |

---

## Support

If you continue to experience issues:

1. Verify all migrations ran successfully: `php artisan migrate:status`
2. Check database schema: `DESCRIBE carts;`, `DESCRIBE products;`, `DESCRIBE orders;` should all show `store_id` column
3. Ensure `app/Store.php` model exists and is properly namespaced
4. Verify no PHP errors: check application error logs in `storage/logs/`

