Laravel 5 预加载从外键中删除前导零
Laravel 5 eager loading dropping leading zeros from foreign key
我相信我在 Laravel 5.3 如何处理当外键是包含 zerofilled 数字的字符串时的预先加载方面遇到了一个错误。
我有两个模型(使用 mysql 后端),School
和 Student
。 School
中的 id
字段是一个字符串,包含由国家分配的 5 位数字,其中包括前导零。 Student
BelongsTo
School
,并包含一个 school_id
字段,其定义与 School
中的 id
字段相同。我做了一些测试,发现当我调用 Student::with('school')
时,只要 Student
中的 school_id
没有前导零,我就会得到预期的 School
模型,但它 returns 没有 School
模型 school_id
带有前导零的值。
我已经用新创建的 School
记录进行了直接测试,两个数据库表中的值都以前导零正确存储,当我尝试直接查询表时,前导零工作正常,但是一旦 with()
进入等式,事情就崩溃了。我试图通过其他方式重现失败,甚至手动构造 whereIn()
调用以镜像由 with()
构造的查询的语法,但其他一切都正确 returns 预期的记录。
此代码在 Laravel 从 4.1 升级到 5.3 之前运行良好,所以我想知道可能发生了什么变化。我已经深入研究了 the GitHub repository for BelongsTo,并且参数处理的 none 似乎去掉了前导零,所以我真的不知道为什么 with()
会闯入这样。
那么,有人可以分享任何见解吗?我很难过,宁愿不必围绕 with()
进行工程设计。我还要预先声明,我不能从 id
字段中删除前导零,这不是一个选项,它们必须实际存储,而不仅仅是像 ZEROFILL 那样显示。
更新:我附上了一个示例,它确定存储在 Student
中的 school_id
在与 with()
分开使用时可以成功连接到相应的 School
声明:
$interventions = Intervention::with('school')->where('id','=',780)->get();
$schools = School::whereIn('id',$interventions->pluck('school_id')->all())->get();
throw new \Exception(print_r($interventions,true).'|'.print_r($schools,true));
以下是 \Exception 的(为简洁起见进行了编辑)结果:
Exception in AdminController.php line 273:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\Student Object
(
[attributes:protected] => Array
(
[id] => 780
[school_id] => 01234
)
[relations:protected] => Array
(
[school] =>
)
)
)
)
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\School Object
(
[attributes:protected] => Array
(
[id] => 01234
[school] => Test1
[district_id] => 81000
[inactive] => 0
[see] => 0
)
)
)
)
因此,虽然 Student::with('school')
未能提取相应的 School
,但将相同的 Student->school_id
值提供给 School::whereIn()
成功。我还是一头雾水。
您没有显示模型 类,但我猜您需要 School
Eloquent 模型中的 public $incrementing = false;
。否则在匹配关系时会被强制转换为int,丢失所有前导零。
我相信我在 Laravel 5.3 如何处理当外键是包含 zerofilled 数字的字符串时的预先加载方面遇到了一个错误。
我有两个模型(使用 mysql 后端),School
和 Student
。 School
中的 id
字段是一个字符串,包含由国家分配的 5 位数字,其中包括前导零。 Student
BelongsTo
School
,并包含一个 school_id
字段,其定义与 School
中的 id
字段相同。我做了一些测试,发现当我调用 Student::with('school')
时,只要 Student
中的 school_id
没有前导零,我就会得到预期的 School
模型,但它 returns 没有 School
模型 school_id
带有前导零的值。
我已经用新创建的 School
记录进行了直接测试,两个数据库表中的值都以前导零正确存储,当我尝试直接查询表时,前导零工作正常,但是一旦 with()
进入等式,事情就崩溃了。我试图通过其他方式重现失败,甚至手动构造 whereIn()
调用以镜像由 with()
构造的查询的语法,但其他一切都正确 returns 预期的记录。
此代码在 Laravel 从 4.1 升级到 5.3 之前运行良好,所以我想知道可能发生了什么变化。我已经深入研究了 the GitHub repository for BelongsTo,并且参数处理的 none 似乎去掉了前导零,所以我真的不知道为什么 with()
会闯入这样。
那么,有人可以分享任何见解吗?我很难过,宁愿不必围绕 with()
进行工程设计。我还要预先声明,我不能从 id
字段中删除前导零,这不是一个选项,它们必须实际存储,而不仅仅是像 ZEROFILL 那样显示。
更新:我附上了一个示例,它确定存储在 Student
中的 school_id
在与 with()
分开使用时可以成功连接到相应的 School
声明:
$interventions = Intervention::with('school')->where('id','=',780)->get();
$schools = School::whereIn('id',$interventions->pluck('school_id')->all())->get();
throw new \Exception(print_r($interventions,true).'|'.print_r($schools,true));
以下是 \Exception 的(为简洁起见进行了编辑)结果:
Exception in AdminController.php line 273:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\Student Object
(
[attributes:protected] => Array
(
[id] => 780
[school_id] => 01234
)
[relations:protected] => Array
(
[school] =>
)
)
)
)
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\School Object
(
[attributes:protected] => Array
(
[id] => 01234
[school] => Test1
[district_id] => 81000
[inactive] => 0
[see] => 0
)
)
)
)
因此,虽然 Student::with('school')
未能提取相应的 School
,但将相同的 Student->school_id
值提供给 School::whereIn()
成功。我还是一头雾水。
您没有显示模型 类,但我猜您需要 School
Eloquent 模型中的 public $incrementing = false;
。否则在匹配关系时会被强制转换为int,丢失所有前导零。