代码之家  ›  专栏  ›  技术社区  ›  Mohamed Raza

在Laravel关系对象上将字符串转换为整数

  •  0
  • Mohamed Raza  · 技术社区  · 3 年前

    我有一个数组的集合,它由作为字符串的数量组成,我想要的是删除 $ 符号,并将字符串变为整数

    "collection": [
            {
                "id": 983,
                "controlNumber": "4498818",
                "ItemNumber": "PS2W12077",
                "shipment": {
                    "id": 27,
                    "Item": "PS2W12077",
                    "ColorCode": "GRPFR",
                    "qty": "$1,638"
                }
            },
            {
                "id": 982,
                "controlNumber": "4498818",
                "ItemNumber": "PS2W12077",
                "shipment": {
                    "id": 27,
                    "Item": "PS2W12077",
                    "ColorCode": "GRPFR",
                    "qty": "$1,638"
                }
            },
            {
                "id": 981,
                "controlNumber": "4498818",
                "ItemNumber": "PS2W12077",
                "shipment": {
                    "id": 27,
                    "Item": "PS2W12077",
                    "ColorCode": "GRPFR",
                    "qty": "$1,638"
                }
            },
            {
                "id": 980,
                "controlNumber": "4498818",
                "ItemNumber": "PS2W12077",
                "shipment": {
                    "id": 27,
                    "Item": "PS2W12077",
                    "ColorCode": "GRPFR",
                    "qty": "$1,638"
                }
            }
    ]
    

    我在模型上建立关系的方式如下

        public function Openorder()
        {
            $object = $this->hasOneThrough(OpenorderImportTemp::class, 'ItemNumber',  'Item');
            return $object;
        }
    

    然后在下面的控制器上获取

    $openorder_data = OpenorderImportTemp::
            select('id', 'controlNumber', 'ItemNumber', )
                ->with('shipment:id,Item,ColorCode,qty')
                // ->groupBy('controlNumber')
                ->orderby('id', 'desc')
                ->where('ControlNumber', '!=', '0')
                ->get();
    

    将字符串更改为整数的任何可能性。我试过的在下面

    $openorder_data = OpenorderImportTemp::
            select('id', 'controlNumber', 'ItemNumber', DB::raw("SUM(shipment.qty) as qtysum") )
                ->with('shipment:id,Item,ColorCode,qty')
                // ->groupBy('controlNumber')
                ->orderby('id', 'desc')
                ->where('ControlNumber', '!=', '0')
                ->get();
    
    1 回复  |  直到 3 年前
        1
  •  0
  •   Mohamed Raza    3 年前
    protected $casts = [
            'qty' => 'integer',
        ];
    

    在模型上解决我的问题。将字符串更改为integer

    推荐文章