:::
17-4 我的訂單列表
- 修改路由
\專案\routes\web.php,在裡面加入訂單列表的路由設定Route::get('/order', 'OrderController@index')->name('order.index'); - 修改選單
\專案\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> - 修改控制器
\專案\app\Http\Controllers\OrderController.php,我們來讀出目前這個使用者的訂單public function index(Request $request) { $user = $request->user(); return view('order.index', compact('user')); } - 接著順便修改轉向,以便送出訂單後,就可以直接看到訂單列表
public function store(Request $request) { ...略... return redirect()->route('order.index'); } - 最後建立訂單視圖
\專案\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
17-3 加入送出訂單按鈕
