Laravel Relationship查询

时间:2017-07-11 06:10:05

标签: php mysql laravel laravel-5.4 laravel-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时,我必须提供订单的订单详情(产品和数量),在我的控制器中,我能够使用order_details使用关系products >

        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
        }
    ]
}

有没有办法可以使用查询

获得上述响应

谢谢

2 个答案:

答案 0 :(得分:3)

使用来自collectionmapflatten即可。

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

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

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

答案 1 :(得分:0)

此查询准确提供了我需要的内容

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"
        }
    ]
}