在 Laravel 关系对象上将字符串转换为整数
Convert String to integer on Laravel relationship object
我有一个数组集合,其中包含 qty 作为字符串我想要的是删除一个 $
符号并将字符串设为整数
"collection": [
{
"id": 983,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",638"
}
},
{
"id": 982,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",638"
}
},
{
"id": 981,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",638"
}
},
{
"id": 980,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",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();
protected $casts = [
'qty' => 'integer',
];
模型解决了我的问题。将字符串更改为整数
我有一个数组集合,其中包含 qty 作为字符串我想要的是删除一个 $
符号并将字符串设为整数
"collection": [
{
"id": 983,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",638"
}
},
{
"id": 982,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",638"
}
},
{
"id": 981,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",638"
}
},
{
"id": 980,
"controlNumber": "4498818",
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": ",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();
protected $casts = [
'qty' => 'integer',
];
模型解决了我的问题。将字符串更改为整数