laravel - 渴望加载过滤器(where 子句)?
问题描述
是否可以将 where 子句添加到模型上的急切加载方法中?例如:
class User extends Model {
public function sources()
{
return $this->hasManyThrough(
Source::class, // The related model
UserNetwork::class, // The intermediate table that connects this model with the related model.
'user_id', // The intermediate table's column that connects to this model.
'network_id', // The related table's column that connects to the secondLocalKey.
'id', // This model's column that connects to the firstKey.
'network_id' // The intermediate table's column that connects the related model.
)
->where('xyz_id', $this->xyz_id);
}
}
这样做会User::with('sources')->get()
返回具有源关系的用户集合,但源集合为空。当我运行User::first()->sources()->get()
它时,它只返回带有实际数据的源集合。
急切加载不应该“急切加载”所有记录,而不是必须指定 first() 记录吗?
解决方案
推荐阅读
- php - 如何将命名方法传递给 Laravel 中的后备路由?
- python - Kivy - 使用按钮开始视频
- ios - 为 3D 模型的某些部分添加纹理
- amazon-web-services - s3 或 cloudfront 如何计算我的带宽消耗?
- python - 未使用 gitlab API 显示的所有组
- rust - 使用 rustc 编译代码时如何在库目录之外调用 Rust 代码?
- asp.net-core - Swagger 显示来自其他项目的端点
- amazon-web-services - 无法在食谱中创建文件
- ros - DJI M600Pro 模拟器 ROS - 地理围栏?
- symfony4 - 如何使用同一网络从另一台计算机访问 symfony 4 本地项目