Laravel 路由绑定模型

Published on 07 September 2020 By. ghost

通过路由获取模型自定义键

    /**
     * 获取该模型的路由的自定义键名
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'order_number';
    }

Controller 模型注

/**
     * 获取订单详情
     *
     * @param Order $order
     * @return mixed
     */
    public function index(Order $order)
    {
        return $this->success($order->formattingData());
    }

Route 添加路由绑定

$route->get('/{order}','Api\OrderController@index'); //获取订单

通过订单号请求获取模型数据

>> {{local}}api/v1/order/202009062223581087099058

返回值

{
    "status": "success",
    "code": 200,
    "data": {
        "order_number": "202009062223581087099058",
        "user_info": {
            "id": 89,
            "name": "张三",
            "identity_number": "340708********1010"
        },
        "message": null,
        "image": [],
        "contact_person": "张三",
        "contact_number": "189*********",
        "estimated_price": "0.00",
        "contact_address": "",
        "price": null,
        "goods_category": [
            {
                "id": 4,
                "name": "电视",
                "pivot": {
                    "order_id": 86,
                    "goods_category_id": 4
                }
            }
        ],
        "order_info": [
            {
                "goods_category_id": 4,
                "brand": null,
                "spec": null,
                "num": 1,
                "price": "-1.00",
                "is_complete": 0
            }
        ],
        "status": "派单中",
        "ticket": "https://localhost.test/images/original/images/88832f6284063acf6b2561ab81f2beb5.png"
    }
}
0条评论

发表评论

您的电子邮件地址不会被公布。必填字段被标记为*