laravel - 使用 with() 从多个表中选择特定列并选择 ()
问题描述
我有 2 张桌子test
和goals
. 一个目标有许多测试。我想获取如下数据。
$tests = test::with('goals')
->where('Goal_id', $goal_id)
->select('test.id as id','goals.Goal_id as goal_id','test.mode as mode')
->get();
但我收到错误Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'goals.Goal_id' in 'field list'
解决方案
目标关系被预先加载到单独的查询中,因此您将无法goals.Goal_id
在主查询构建器中访问,而是可以修改with()
子句以从预先加载的关系中选择特定列作为with('goals:Goal_id,another_column')
$tests = test::with('goals:Goal_id')
->where('Goal_id', $goal_id)
->select(['test.id as id','test.mode as mode'])
->get();
推荐阅读
- types - Cython C 型定义
- angularjs - Media.net 广告与 SPA(angularjs) 的集成
- multithreading - 带有 Jedis 连接工厂、Redis 独立配置和多线程的 Spring Redis 模板
- php - 在 php 7.1 上安装了 mbstring 但 phpinfo 说它没有安装 - CentOS 7
- python - 找到重复项时如何更新元组
- c++ - 在 MSDOS 编译器上初始化一个大于最大值的 int 时输出的混乱
- c# - 向默认 Index.cshtml RazorPages Asp.Net Core 2.1.0 添加一个属性
- java - Android facebook-sdk 在土耳其语中崩溃
- javascript - 点击时的 jQuery 手风琴事件
- c# - c# WPF image show tiff (just 1 page) blurry