我想要获得至少有一个孩子的所有优惠券,一张优惠券可以有多个优惠券孩子,但任何优惠券只能有一个父母。
我使用以下模型和调用对其进行设置,并且它生成的查询是根据需要的,直到这一部分:'vouchers'.'parent_id' = 'vouchers'.'id'
想要的功能:
$vouchers = Voucher::has('children')->get();
or
$vouchers = Voucher::has('parent')->get();
结果查询
select * from `vouchers` where `vouchers`.`deleted_at` is null
and (select count(*) from `vouchers` where `vouchers`.`deleted_at` is null
and `vouchers`.`parent_id` = `vouchers`.`id`
and `vouchers`.`deleted_at` is null ) >= 1
Models:
class Voucher extends baseModel {
public function parent()
{
return $this->belongsTo('Voucher', 'parent_id');
// return $this->belongsTo('Voucher', 'parent_id', 'id'); <- attempted but din't work
}
public function children()
{
return $this->hasMany('Voucher', 'parent_id');
}
}
此问题已在 5.0 中报告并修复https://github.com/laravel/framework/pull/8193 https://github.com/laravel/framework/pull/8193
不幸的是,版本 4 没有向后移植。
但是,如果您想自己应用修复程序,您可以在此处查看修改列表:https://github.com/laravel/framework/pull/8193/files https://github.com/laravel/framework/pull/8193/files
请注意,修改框架的代码库是有风险的,但 Laravel 4.x 版本不会再有错误修复,只有几个月的安全修复。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)