php - Laravel 关系查询

标签 php mysql laravel laravel-5.4 eloquent

我有 2 个模型 OrderDetailsProduct

//OrderDetail MOdel 

    class OrderDetail extends Model
    {
        protected $fillable = ['order_id', 'product_id', 'quantity'];

        protected $hidden = ['created_at','updated_at','deleted_at'];

        public function product() 
        {
            return $this->hasMany('App\Product', 'id');
        }
    }

 //OrderDetails Migration

    Schema::create('order_details', function (Blueprint $table) {
        $table->increments('id');
        $table->string('order_id');
        $table->string('product_id');
        $table->integer('quantity');
        $table->timestamps();
    });

//Products Model

    class Product extends Model
    {
        use SoftDeletes;

        protected $fillable = ['name'];

        protected $hidden = ['created_at','updated_at','deleted_at'];

        protected $dates = ['deleted_at'];

        public function productCategory()
        {
            return $this->belongsTo('App\ProductCategory', 'category_id');
        }

        public function orderDetail()
        {
            return $this->belongsTo('App\OrderDetail');
        }
    }

//products migration
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('proportion');
        $table->string('mrp');
        $table->string('price');
        $table->string('category_id');
        $table->string('description')->nullable();
        $table->string('image_small')->nullable();
        $table->string('image_zoom')->nullable();
        $table->enum('trending', ['yes','no']);
        $table->enum('status', ['available','unavailable']);
        $table->timestamps();
        $table->softDeletes();
    });

当用户通过 Order_id 时,我必须提供订单的订单详细信息(产品和数量),在我的 Controller 中我能够返回 order_detailsproducts 使用关系

        public function getDbOrderDetails($order_id)
        {
            $get_orders_details = OrderDetail::where('order_id', $order_id)
                                             ->with('product')
                                             ->get(); 

            $data = [
                'data' => $get_orders_details
            ];

            return Response::json(
                $data_with_status = array_merge($this->respondSuccess('query successfull'), $data)
            );
        }

    //response
{
    "status_code": 200,
    "status_message": "query successfull",
    "data": [
        {
            "id": 1,
            "name": "Chicken Leg Piece",
            "proportion": "1 kg",
            "mrp": "200",
            "price": "185",
            "category_id": "2",
            "description": "Description about the product",
            "image_small": "Sign.jpg",
            "image_zoom": "Sign.jpg",
            "trending": "no",
            "status": "available"
        },
        2,  //should be inside the above object
        {
            "id": 2,
            "name": "Chicken Leg Piece",
            "proportion": "2 kg",
            "mrp": "425",
            "price": "400",
            "category_id": "1",
            "description": "Description about the product",
            "image_small": "Sign.jpg",
            "image_zoom": "Sign.jpg",
            "trending": "yes",
            "status": "available"
        },
        3  //should be inside the above object
    ]
}

问题出在我得到的response上,很难在前端应用程序中解析json,所以我想要如下响应

{
    "status_code": 200,
    "status_message": "query successfull",
    "data": [
        {
            "id": 1,
            "name": "Chicken Leg Piece",
            "proportion": "1 kg",
            "mrp": "200",
            "price": "185",
            "category_id": "2",
            "description": "Description about the product",
            "image_small": "Sign.jpg",
            "image_zoom": "Sign.jpg",
            "trending": "no",
            "status": "available",
            "quantity": 2,            //from order details table
        },
        {
            "id": 2,
            "name": "Chicken Leg Piece",
            "proportion": "2 kg",
            "mrp": "425",
            "price": "400",
            "category_id": "1",
            "description": "Description about the product",
            "image_small": "Sign.jpg",
            "image_zoom": "Sign.jpg",
            "trending": "yes",
            "status": "available",
            "quantity": 4,           //from order details table
        }
    ]
}

有没有一种方法可以使用查询获得上述响应

谢谢

最佳答案

使用来自collectionmapflatten会做的。

$mapped = $get_orders_details->map(function($v) {
    $v->product->quantity = $v->quantity;

    return $v->product;
})
->flatten()
->all();

$data = [
    'data' => $mapped
];

关于php - Laravel 关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45026663/

相关文章:

php - 如何实现输出id和更新表

php - 一切正常,但注册不起作用

mysql - PHP 7 性能

Laravel 如何根据用户模型上的额外标志防止用户登录?

php - 错误: Extra content at the end of the document Below is a rendering of the page up to the first error

php - 单击提交按钮时,保留并传递在 javascript 中创建的动态生成的 html 表到 php

php - 用PHP提交MYSQL语法错误

mysql - SQL select true if 日期比较

laravel - 如何对 json 数据使用 Symfony 验证

laravel - HtmlServiceProvider.php 第 36 行中的 FatalErrorException : laravel