gpt4 book ai didi

php - 无法在 Advanced::with() 语句中选择列

转载 作者:行者123 更新时间:2023-11-29 12:46:21 24 4
gpt4 key购买 nike

我在使用高级 query::with 选择列(MySQL SELECT 语句)时遇到问题声明。

Property::where('public_id', $publicId)->with(['owner' => function ($query) {
$query->select('email'));
}])->first();

如果我添加 select 语句,则返回的 owner 对象为 null,如果添加完整的 User 模型,则返回的 owner 对象为 null我没有添加 select('...') 语句。

如果我从 phpMyAdmin 运行查询,执行的查询是正确的并返回所需的数据:

select * from `properties` where `public_id` = '53f1c59cefe65' limit 1;
select `email` from `users` where `users`.`id` in ('54');

未选择的数据:

{"id":4,"owner_id":54,"owner":{"id":54,"username":"ckertzmann","email":"ckertzmann@gmail.com","permissions":[],"activated":true,"activated_at":null,"last_login":null,"first_name":"Daisy","last_name":"Haag","created_at":"2014-08-18 09:21:26","updated_at":"2014-08-18 09:21:26"}}

选择数据:

{"id":4,"owner_id":54,"owner":null}

最佳答案

您需要选择键,以便 Eloquent 知道如何将相关模型与其各自的关系父级相匹配。

Property::where('public_id', $publicId)->with(['owner' => function ($query) {
$query->select('email', 'owner_id');
}])->first();

关于php - 无法在 Advanced::with() 语句中选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25361832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com