Home  >  Article  >  PHP Framework  >  How to use ThinkPHP6 to implement an order management system

How to use ThinkPHP6 to implement an order management system

PHPz
PHPzOriginal
2023-06-20 08:42:461864browse

With the development of the Internet and the rise of e-commerce, more and more companies are beginning to use online order management systems to better manage orders, improve work efficiency, and provide better customer service. This article will introduce how to use the ThinkPHP6 framework to develop a simple order management system, covering order list, order details, search, sorting, paging and other functions.

  1. Preparation work

First, you need to install PHP, MySQL and Composer. After installing these necessary components, you can install ThinkPHP6. If you are not familiar with ThinkPHP6, you can read the official documentation or refer to some related tutorials.

  1. Create database and data tables

Before you start developing the order management system, you need to create a database. Create a database named "order_system" in MySQL, and then create a data table named "orders".

The data table contains the following fields:

id - order ID

customer_name - customer name

customer_email - customer email

product_name - product name

product_price - product price

product_quantity - product quantity

created_at - order creation time

updated_at - order update time

  1. Create models and controllers

In ThinkPHP6, a model corresponds to a data table, and a controller corresponds to a route.

First, create a model named "Order", which will correspond to the "orders" data table.

php artisan make:model Order -m

Add to the injected code

namespace appmodel;

use thinkModel;

class Order extends Model
{

}

Then, create a controller named "Order" and the "index" method for displaying the order list view.

php artisan make:controller Order --resource

Add to Action, code

public function index()
{
    $orders = Order::paginate(10);
    return view('order/index', ['orders' => $orders]);
}
  1. Create view

Next, create a view named "index.blade.php" , used to display the order list, which includes search, sorting and paging functions.

First, create a view file named "index.blade.php" in the "order" directory, and then add the following code:

@extends('layout')

@section('content')
    <h2>订单列表</h2>

    <form action="{{route('orders.index')}}" method="get">
        <div class="form-group">
            <input type="text" name="q" value="{{$q}}" class="form-control" placeholder="搜索">
        </div>

        <button type="submit" class="btn btn-primary">搜索</button>
    </form>

    <table class="table">
        <thead>
        <tr>
            <th>ID</th>
            <th>客户姓名</th>
            <th>客户电子邮件</th>
            <th>产品名称</th>
            <th>产品价格</th>
            <th>产品数量</th>
            <th>订单创建时间</th>
            <th></th>
        </tr>
        </thead>
        <tbody>
        @foreach ($orders as $order)
            <tr>
                <td>{{$order->id}}</td>
                <td>{{$order->customer_name}}</td>
                <td>{{$order->customer_email}}</td>
                <td>{{$order->product_name}}</td>
                <td>{{$order->product_price}}</td>
                <td>{{$order->product_quantity}}</td>
                <td>{{$order->created_at}}</td>
                <td><a href="{{route('orders.show', $order->id)}}" class="btn btn-primary">详情</a></td>
            </tr>
        @endforeach
        </tbody>
    </table>

    {{$orders->links()}}
@endsection

In this view, the Bootstrap style is used , also adds a search box and a paging control.

  1. Create order details method and view

Then, create a method named "show" and a view named "show.blade.php" for Display order details.

Add the following code in the "Order" controller:

public function show($id)
{
    $order = Order::findOrFail($id);
    return view('order/show', ['order' => $order]);
}

Create a view file named "show.blade.php" in the "order" directory and add the following code:

@extends('layout')

@section('content')
    <h2>订单详情</h2>

    <table class="table">
        <tbody>
        <tr>
            <th>ID</th>
            <td>{{$order->id}}</td>
        </tr>
        <tr>
            <th>客户姓名</th>
            <td>{{$order->customer_name}}</td>
        </tr>
        <tr>
            <th>客户电子邮件</th>
            <td>{{$order->customer_email}}</td>
        </tr>
        <tr>
            <th>产品名称</th>
            <td>{{$order->product_name}}</td>
        </tr>
        <tr>
            <th>产品价格</th>
            <td>{{$order->product_price}}</td>
        </tr>
        <tr>
            <th>产品数量</th>
            <td>{{$order->product_quantity}}</td>
        </tr>
        <tr>
            <th>订单创建时间</th>
            <td>{{$order->created_at}}</td>
        </tr>
        <tr>
            <th>订单更新时间</th>
            <td>{{$order->updated_at}}</td>
        </tr>
        </tbody>
    </table>

    <a href="{{route('orders.index')}}" class="btn btn-primary">返回</a>
@endsection
  1. Add search, sorting and paging functions

In order to implement search, sorting and paging functions, the "index" method needs to be modified.

Add the following code in the "index" method of the "Order" controller:

public function index(Request $request)
{
    $q = $request->input('q');

    $orders = Order::when($q, function ($query) use ($q) {
            $query->where('customer_name', 'like', "%$q%")
                ->orWhere('customer_email', 'like', "%$q%")
                ->orWhere('product_name', 'like', "%$q%");
        })
        ->orderBy('created_at', 'desc')
        ->paginate(10)
        ->appends(['q' => $q]);

    return view('order/index', ['orders' => $orders, 'q' => $q]);
}

In this code, the IlluminateSupportFacadesRequest class is used to obtain the search parameter "q", and " orderBy" method to sort in reverse order of creation time. Then, use the "paginate" method to paginate, and the "appends" method to add search parameters to the paginated link.

  1. Test

Now you can test the created order management system. Enter http://localhost/orders in the browser to see the order list. After entering keywords and clicking the search button, you can see the search results; after clicking the details button, you can view the order details. Below the pagination link, you can see the paging controls.

Summary

At this point, we have completed all the steps to create a simple order management system using the ThinkPHP6 framework. This article describes how to create data tables, models, and controllers, and shows how to use view files to render order data, search, sort, and paginate. By studying this tutorial, you can have a deeper understanding of the ThinkPHP6 framework and be able to use the ThinkPHP6 framework to develop your own business system.

The above is the detailed content of How to use ThinkPHP6 to implement an order management system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn