Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hira4472's avatar

How to get Array of Orders and Sub Array of Product Details in Each Array of Orders?

First I wanted to show all orders of specific customer so I did this:


  $finaldata = array();
        $finaldata['status']="success";
        $finaldata['reason']="record fetched successfully";
        $orderdetail=orderdetail::where('OrderID',$id)->get();
        
        
        $finaldata['order']=$orderdetail;


        return response()->json($finaldata);

It is giving me this response in postman

{
    "status": "success",
    "reason": "record fetched successfully",
    "order": [
        {
            "OrderDetailID": 1,
            "OrderDate": "12-1-2020",
            "StatusID": 1,
            "ProductID": 3,
            "OrderID": 1
        },
        {
            "OrderDetailID": 2,
            "OrderDate": "12-1-2020",
            "StatusID": 1,
            "ProductID": 2,
            "OrderID": 1
        }
    ]
}

Now I don't want to display the product, its details, and status details instead of product ID, and Status ID. How would i do that?

I have also made relationship among tables.

class product extends Model
{protected $hidden = ['created_at','updated_at','remember_token'];
    public $timestamps = false;
    protected $table='products';
    protected $primaryKey ="ProductID";
    protected $fillable=["ProductName","SubCatID","YardID","SizeID","ColorID","DescriptionID","ProductPrice","Image"];

    public function yard()
    {
        return $this->hasOne(yard::class,'YardID',"YardID");
    }
    public function color()
    {
        return $this->hasOne(color::class,'ColorID',"ColorID");
    }
    public function description()
    {
        return $this->hasOne(description::class,'DescriptionID',"DescriptionID");
    }
    public function size()
    {
        return $this->hasOne(size::class,'SizeID',"SizeID");
    }

}

class orderdetail extends Model
{

    public $timestamps=false;
    protected $guarded=[];
    protected $primaryKey = 'OrderDetailID';
    protected $hidden = ['created_at','updated_at','remember_token'];

    public  function shippingdetail()
    {
        return $this->hasOne(shippingdetail::class,'OrderDetailID','OrderDetailID');
    }

    public  function statusdetail()
    {
        return $this->belongsTo(statusdetail::class,'StatusID','StatusID');
    }

    public  function order()
    {
        return $this->belongsTo(order::class,'OrderID','OrderID');
    }
    public  function product()
    {
        return $this->belongsToMany(product::class,'ProductID','ProductID');
    }
}
class statusdetail extends Model
{
    public $timestamps=false;
    protected $guarded=[];
    protected $primaryKey = 'StatusID';
    protected $hidden = ['created_at','updated_at','remember_token'];

    public  function orderdetail()
    {
        return $this->hasOne(orderdetail::class,'StatusID','StatusID');
    }
}

class order extends Model
{
    public $timestamps=false;
    protected $guarded=[];
    protected $primaryKey = 'OrderID';
    protected $hidden = ['created_at','updated_at','remember_token'];

    public  function orderdetail()
    {
        return $this->hasMany(orderdetail::class,'OrderID','OrderID');
    }

    public  function customerdetail()
    {
        return $this->belongsTo(customerdetail::class,'CustomerID','CustomerID');
    }

    public  function paymentdetail()
    {
        return $this->belongsTo(paymentdetail::class,'PaymentID','PaymentID');
    }

}
0 likes
0 replies

Please or to participate in this conversation.