hasOne 关联查询外键值是 0 开头的,关联查不出数据????????????

return $this->hasOne(‘App\Models\BaiyingRegion’, ‘code’, ‘district’);

code和district 是010 ,0开头关联不出数据,需要做省市区关联查询,但是省ID,code是0开头的,就查不数据,不知道什么原因,大家有什么解决方案。感谢

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

问题已解决
控制器:
$res = UserAddress::where(‘user_id’, $user_id);
$res = $res->with([
‘getBaiyingRegionCity’ => function ($query) {
$query->select(‘code’, ‘parent_code’,’name’,’level’);
},
]);
$res = $res->orderBy(‘address_id’);
$res = $this->baseRepository->getToArrayGet($res);
var_dump($res);die;
model层:
public function getBaiyingRegionCity()
{
return $this->hasOne(‘App\Models\BaiyingRegion’, “code”, “city”);
}
这样写getBaiyingRegionCity 没有赋值成功;
把 $query->select(‘code’, ‘parent_code’,’name’,’level’); 需要查询的字段放到model里面就可以了;
public function getBaiyingRegionCity()
{
return $this->hasOne(‘App\Models\BaiyingRegion’, “code”, “city”)->select(array(‘code’, ‘name’));
}
这样处理后就可以赋值成功了。

3年前 评论
讨论数量: 3

建议把SQL打印出来 再进行判断

3年前 评论

sql打出来没有问题

3年前 评论

问题已解决
控制器:
$res = UserAddress::where(‘user_id’, $user_id);
$res = $res->with([
‘getBaiyingRegionCity’ => function ($query) {
$query->select(‘code’, ‘parent_code’,’name’,’level’);
},
]);
$res = $res->orderBy(‘address_id’);
$res = $this->baseRepository->getToArrayGet($res);
var_dump($res);die;
model层:
public function getBaiyingRegionCity()
{
return $this->hasOne(‘App\Models\BaiyingRegion’, “code”, “city”);
}
这样写getBaiyingRegionCity 没有赋值成功;
把 $query->select(‘code’, ‘parent_code’,’name’,’level’); 需要查询的字段放到model里面就可以了;
public function getBaiyingRegionCity()
{
return $this->hasOne(‘App\Models\BaiyingRegion’, “code”, “city”)->select(array(‘code’, ‘name’));
}
这样处理后就可以赋值成功了。

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!