Laravel关系查询

Laravel Relationship query

我有 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 时,我必须提供订单的详细信息(产品和数量),在我的控制器中我能够 return 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
        }
    ]
}

有没有一种方法可以使用查询

获得上述响应

谢谢

使用 mapcollection 中的 flatten 即可。

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

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

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

这个查询正是我所需要的

public function getDbOrderDetails($order_id)
{
    $get_orders_details = OrderDetail::join('products', 'products.id', '=', 'order_details.product_id')
        ->where('order_id', $order_id)
        ->select('name', 'proportion', 'mrp', 'price', 'quantity', 'image_small', 'image_zoom')
        ->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": [
        {
            "name": "Chicken Leg Piece",
            "proportion": "1 kg",
            "mrp": "200",
            "price": "185",
            "quantity": 2,
            "image_small": "Sign.jpg",
            "image_zoom": "Sign.jpg"
        },
        {
            "name": "Chicken Leg Piece",
            "proportion": "2 kg",
            "mrp": "425",
            "price": "400",
            "quantity": 3,
            "image_small": "Sign.jpg",
            "image_zoom": "Sign.jpg"
        }
    ]
}