laravel - Laravel where in a relational database
问题描述
I have two tables Thread and Participants Thread has many participants and I'm able to access it like so:
Thread::with('participants')->get();
The participants
table has a column last_read
I would like to get all participants from thread WHERE last_read is NULL
. I've tried this but it returns an error.
Thread::with('participants')
->where('last_read', NULL)
->get();
I am getting this error:
ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'last_read' in 'where clause' (SQL: select * from
threads
wherelast_read
is null andthreads
.deleted_at
is null)
Any idea how I can do this?
解决方案
Since you want a query on relation table use whereHas
Thread::with('participants')
->whereHas('participants', function($q){
$q->where('last_read', NULL);
})->get();
推荐阅读
- ios - iOS:无效更新:第 0 节中的行数无效
- html - v-on 处理程序中的错误:“TypeError:无法读取未定义的属性 '$createElement'”
- ruby-on-rails - 保存前计算嵌套项
- delphi - 获取组权限的文件/文件夹列表
- cypress - 在赛普拉斯中,何时使用自定义命令与任务?
- django - Django 模板化不显示要填充的输入
- javascript - 喜欢和不喜欢 javascript 和 Laravel
- tensorflow - 哪个 TFX 编排器实际上是 TFX 的标准?
- php - 在 PHP 7.2 开发服务器上使用 router.php 脚本来处理 .htaccess mod 重写
- python - “VoiceClient”对象没有属性“create_ffmpeg_player”