iT邦幫忙

2025 iThome 鐵人賽

DAY 25
0
Modern Web

Laravel 是甚麼系列 第 25

做每頁畫面都相同的導航列partial_view

  • 分享至 

  • xImage
  •  

做每頁畫面都相同的導航列partial_view
加入資料夾
https://ithelp.ithome.com.tw/upload/images/20250918/201190356t2WPhYBNk.png
加入檔案
https://ithelp.ithome.com.tw/upload/images/20250918/20119035wP4jIgXCXe.png
填入
https://ithelp.ithome.com.tw/upload/images/20250918/20119035wQZv6zVyuf.png
程式碼

<html>
    <head>
        <title>電商網站</title>
        </head>
    <body>
    <div>
            @yield('content')
        </div>
    </body>
</html>

再新增檔案+貼上

<div>
  <a href="/">商品列表</a>
  <a href="/contact-us">聯絡我們</a>
</div>

https://ithelp.ithome.com.tw/upload/images/20250918/20119035I2ZZxWKRZP.png
再修改app.blade.php程式碼
https://ithelp.ithome.com.tw/upload/images/20250918/20119035M0248fTuE4.png
再修改 程式碼放到section裡面
https://ithelp.ithome.com.tw/upload/images/20250918/20119035SNGu9Jf2LY.png
下面
https://ithelp.ithome.com.tw/upload/images/20250918/201190359Hu6YOwvoe.png
看結果
https://ithelp.ithome.com.tw/upload/images/20250918/20119035Jpy3XMkYJm.png
另一個
https://ithelp.ithome.com.tw/upload/images/20250918/20119035Py9376GB5T.png

程式碼


@extends('layouts.app')
@section('content')
<h3>聯絡我們</h3>
<form action="">
  請問你是:<input type=text><br>
  請問你的消費時間:<input type="date"><br>
  請問你的消費種類:
  <select name="" id="">
    <option value="物品">物品</option>
    <option value="食物">食物</option>
  </select>
    <button>送出</button>
</form>
@endsection

https://ithelp.ithome.com.tw/upload/images/20250918/20119035EdEKyTwt55.png


加入後臺清單
加入2層資料夾
https://ithelp.ithome.com.tw/upload/images/20250918/20119035vOuc1y4xj5.png
寫裡面的code

<h2 >後台-訂單列表</h2>

<table>
  <thead>
    <tr>
      <td>購買時間</td>
      <td>購買者</td>
      <td>商品清單</td>
      <td>訂單總額</td>
      <td>是否運送</td>
    </tr>
  </thead>
  <tbody>
    @foreach( $orders as $order )
      <tr>
        <td>{{ $order->created_at }}</td>
        <td>{{ $order->user->name }}</td>
        <td>
          @foreach( $order->orderItems as $orderItem )
            {{ $orderItem->product->title }} &nbsp;
          @endforeach
        </td>
        <td>{{ isset($order->orderItems) ? $order->orderItems->sum('price') : 0 }}</td>
        <td>{{ $order->is_shipped }}</td>
      </tr>
    @endforeach
  </tbody>
</table>

https://ithelp.ithome.com.tw/upload/images/20250918/20119035XNTeJ9WDdO.png
下語法: php artisan make:controller OrderController
加入controller
https://ithelp.ithome.com.tw/upload/images/20250918/20119035Mr9Rq7iyum.png
同畫面要多一層資料夾:
https://ithelp.ithome.com.tw/upload/images/20250926/20119035oDzFVywfJw.png
將檔案放入
https://ithelp.ithome.com.tw/upload/images/20250926/20119035L3lfJFz4vS.png
修改程式碼階層,加入controller
https://ithelp.ithome.com.tw/upload/images/20250926/20119035PQhHxXjpg1.png
改程式碼
https://ithelp.ithome.com.tw/upload/images/20250926/20119035uNau37kTm0.png
程式碼內容:

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Order;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    public function index()
    {

              
    $orders = Order::orderBy('created_at', 'desc')->get();

    return view('admin.orders.index',['orders' => $orders]);
  
    }
}

加入路由
https://ithelp.ithome.com.tw/upload/images/20250926/20119035py0Ig25Qb1.png
可以看到資料表的內部結構
選資料表的!再點DDL
https://ithelp.ithome.com.tw/upload/images/20250926/20119035f2ETv4SPws.png
但是 Admin/OrderController 的寫法不正確,應該使用 反斜杠 \ 或者 完整命名空間:
127.0.0.1:8000/admin/orders
https://ithelp.ithome.com.tw/upload/images/20250926/20119035rK0J4WWH6i.png
加入加總訂單總數: {{ $orderCount }}
https://ithelp.ithome.com.tw/upload/images/20250926/20119035CdIytXZRBQ.png
分頁

<div>
  @for ($i = 1; $i <= $orderPages; $i++)
      <a href="/admin/orders?page={{ $i }}">第 {{ $i }} 頁</a> &nbsp;
  @endfor
</div>

修改controller裡面的程式碼
https://ithelp.ithome.com.tw/upload/images/20250926/20119035pWmScvWPur.png

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Order;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    

    public function index(Request $request)
{
    $dataPerPage = 2;
    $orders = Order::orderBy('created_at', 'desc')
                   ->paginate($dataPerPage);
    
    return view('admin.orders.index', [
        'orders' => $orders,
        'orderCount' => Order::count(),
    ]);
}
}

https://ithelp.ithome.com.tw/upload/images/20250926/20119035MeCyMyil7G.png
畫面的程式碼

<h2 >後台-訂單列表</h2>
<span>訂單總數: {{ $orderCount }} </span>
<table>
  <thead>
    <tr>
      <td>購買時間</td>
      <td>購買者</td>
      <td>商品清單</td>
      <td>訂單總額</td>
      <td>是否運送</td>
    </tr>
  </thead>
  <tbody>
    @foreach( $orders as $order )
      <tr>
        <td>{{ $order->created_at }}</td>
        <td>{{ $order->user->name }}</td>
        <td>
          @foreach( $order->orderItems as $orderItem )
            {{ $orderItem->product->title }} &nbsp;
          @endforeach
        </td>
        <td>{{ isset($order->orderItems) ? $order->orderItems->sum('price') : 0 }}</td>
        <td>{{ $order->is_shipped }}</td>
      </tr>
    @endforeach
  </tbody>
</table>
<div>
  @for ($i = 1; $i <= $orders->lastPage(); $i++)
      <a href="{{ $orders->url($i) }}">第 {{ $i }} 頁</a> &nbsp;
  @endfor
</div>

https://ithelp.ithome.com.tw/upload/images/20250926/20119035oPehEP3C8P.png
畫面顯示
127.0.0.1:8000/admin/orders?page=14
https://ithelp.ithome.com.tw/upload/images/20250926/20119035MHpIQcHR3O.png
加入只撈有商品的訂單orderItem

程式碼

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Order;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    

    public function index(Request $request)
{
//
    $dataPerPage = 5;
        
        $orders = Order::with(['orderItems', 'orderItems.product', 'user'])
                      ->orderBy('created_at', 'desc')
                      ->whereHas('orderItems')
                      ->paginate($dataPerPage);
        
        return view('admin.orders.index', [
            'orders' => $orders,
            'orderCount' => Order::whereHas('orderItems')->count(),
        ]);

//顯示所有的訂單
    /*$dataPerPage = 2;
    $orders = Order::orderBy('created_at', 'desc')
                   ->paginate($dataPerPage);
    
    return view('admin.orders.index', [
        'orders' => $orders,
        'orderCount' => Order::count(),
    ]);*/
}
}

127.0.0.1:8000/admin/orders?page=1
https://ithelp.ithome.com.tw/upload/images/20250926/20119035HVmTkWX9H4.png

https://ithelp.ithome.com.tw/upload/images/20250926/20119035p8wnMxFujw.png
大家明天見~


上一篇
加入JS
下一篇
讀檔
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言