:::

17-4 我的訂單列表

  1. 修改路由\專案\routes\web.php,在裡面加入訂單列表的路由設定
    Route::get('/order', 'OrderController@index')->name('order.index');

     

  2. 修改選單\專案\resources\views\layouts\nav.blade.php,加入「我的訂單」以方便點選
    <li class="nav-item">
        <a class="nav-link" href="{{ route('cart.index') }}">我的購物車</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="{{ route('order.index') }}">我的訂單</a>
    </li>

     

  3. 修改控制器\專案\app\Http\Controllers\OrderController.php,我們來讀出目前這個使用者的訂單
    public function index(Request $request)
    {
        $user = $request->user();
        return view('order.index', compact('user'));
    }

     

  4. 接著順便修改轉向,以便送出訂單後,就可以直接看到訂單列表
    public function store(Request $request)
    {
        ...略...
        return redirect()->route('order.index');
    }

     

  5. 最後建立訂單視圖\專案\resources\views\order\index.blade.php
    @extends('layouts.app')
    @section('content')
        <h1>我的訂單<small>(共 {{ count($user->orders) }} 個訂單)</small></h1>
        @forelse($user->orders as $order)
            <div class="card @if($order->closed) border-secondary @else border-info @endif mb-3">
                <div class="card-header text-white @if($order->closed) bg-secondary @else bg-info @endif">
                    訂單日期:{{ $order->created_at}}
                    <span class="float-right">
                        訂單狀態:
                        @if($order->closed)
                            已完成
                        @else
                            處理中
                        @endif
                    </span>
                </div>
                <div class="card-body">
                    <table class="table table-striped">
                        <tr>
                            <th colspan=2>商品名稱</th>
                            <th nowrap class="text-right">商品單價</th>
                            <th nowrap class="text-center">購買數量</th>
                            <th nowrap class="text-right">小計</th>
                        </tr>
                        @foreach($order->items as $item)
                            <tr>
                                <td>
                                    <a target="_blank" href="/product/{{ $item->product_id }}">
                                        <img src="{{ $item->product->image_url }}" class="img-thumbnail" style="width: 120px;">
                                    </a>
                                </td>
                                <td>
                                    <a target="_blank" href="/product/{{ $item->product_id }}">
                                        <h5>{{ $item->product->title }}</h5>
                                    </a>
                                    @if(!$item->product->on_sale)
                                        <div class="warning">该商品已下架</div>
                                    @endif
                                </td>
                                <td class="text-right">
                                    {{ $item->product->price }} 元
                                </td>
                                <td class="text-center">
                                    {{ $item->amount }}
                                </td>
                                <td class="text-right">
                                    {{ $item->product->price * $item->amount }} 元
                                </td>
                            </tr>
                        @endforeach
                        <tr>
                            <th colspan=5 class="text-right">
                                共計 {{ $order->total }} 元
                            </th>
                        </tr>
                    </table>
                </div>
            </div>
        @empty
            <div class="alert alert-info">
                <h2>尚無訂單</h2>
            </div>
        @endforelse
    
    @endsection
    

     

到GitHub觀看此單元程式異動  


:::

書籍目錄

展開 | 闔起

快速登入


https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1454%26tbsn%3D43

計數器

今天: 8598859885988598
昨天: 2489248924892489
總計: 8030580803058080305808030580803058080305808030580