Laravel ORM 如何查询字符串字段?

问题描述 投票:0回答:1
    $orderId = 319769;
    $sql = ShipOrderModel::query()->where('order_id', (string)$orderId);
    // select * from `hr_ship_order` where `order_id` = 319769
    $sql = ShipOrderModel::query()->where('order_id', '319769');
    // select * from `hr_ship_order` where `order_id` = 319769
    $sql = ShipOrderModel::query()->whereRaw('order_id = "?"', $orderId);
    //select * from `hr_ship_order` where order_id = "319769"

ship_order 的字段 order_id 是

varchar
,我知道这难以置信,但是很难改变字段类型。所以当我尝试查询订单id时,我发现类型被转换为整数,这导致表索引不起作用。我尝试了像这样的代码,它仍然不起作用! 请帮助我!请!!!

php laravel orm
1个回答
0
投票

你可以这样尝试。

$orderId = 319769;
$orderId = (string)$orderId;
$sql = ShipOrderModel::query()->where('order_id', '=', $orderId)->get();
© www.soinside.com 2019 - 2024. All rights reserved.