php - 如何在 Laravel QueryBuilder 中基于 json 列加入
问题描述
帖子表
|id|值| |1 |霍格 | |2 |风雅 | |3 |piyo |
some_posts 表
|id|post_ids| |1 |[1, 2] | |2 |[1] | |3 |[1,2,3]|
我想使用QueryBuilder加入。我应该怎么办?
那是不可能的。
DB::table('some_posts')
->join('posts', 'some_posts.post_ids', 'posts.id');
SQLSTATE [42883]:未定义函数:7 错误:运算符不存在:json = integer\n LINE 1: ...ts" 在 "some_posts" 上内连接 "posts"。"post_ids" = "posts".... \n
提示:没有运算符与给定的名称和参数类型匹配。您可能需要添加显式类型转换。(SQL: select "some_posts".* from "some_posts" 内部连接 "posts" on "some_posts"."post_ids" = "posts"."id" where "some_posts"."some_id" = 384)
谢谢,
解决方案
Post
您可以在模型中使用集合
Post.php
Class Post extends model
public function somePosts()
{
return $this->belongsTo('some_posts`, 'post_ids');
}
然后,改变这个
DB::table('some_posts')
->join('posts', 'some_posts.post_ids', 'posts.id');
至
Post::with('somePosts')->get();
推荐阅读
- python - 使用decimal.Decimal从表达式输出多值?
- php - 更改页面后保持聊天打开
- python - * 在 python 中对 range() 有什么作用?
- python-3.x - Discord bot 不能在 Heroku 上使用 Python 运行
- here-api - HERE 路由 API:未找到卡车路线
- grails - 在使用字段 Grails 插件中添加字段
- android - 进入后台时失去活动
- amazon-s3 - 放大 GraphQL 错误“无法读取 null 的属性 'aws_appsync_region'”
- r - 更正 r 中的列值排序
- html - 将 Blockquote 和 p 标签与引号结合起来