英文:
Laravel - select only value from whereExists
问题
我在Laravel中有以下查询:
User::whereIn('id', $ids)
->withExists(['read' => function($q) use($post) {
return $q->where('post_id', $post->id);
}])
->select(['id', 'user_name', 'read_exists'])
->get();
我想只选择'user_name'和是否存在'read_exist'的信息。但是我遇到了"unknown column read_exist"的错误。
那么我如何只选择这三个值,而不是所有用户列?
英文:
I have following query in laravel:
User::whereIn('id', $ids)
->withExists(['read' => function($q) use($post) {
return $q->where('post_id', $post->id);
}]
)->select(['id', 'user_name', 'read_exists'])->get();
And I would like to select only 'user_name' and information if has any 'read_exist'.
But I have error "unknown column read_exist".
So How can I select only this three values, not all user columns?
Thank you.
答案1
得分: 1
将select
方法放在withExists
之前:
User::whereIn('id', $ids)
->select(['id', 'user_name', 'read_exists'])
->withExists(['read' => function($q) use($post) {
return $q->where('post_id', $post->id);
}])
->get();
在文档中有说明。
英文:
Put the select method before withExist
User::whereIn('id', $ids)
->select(['id', 'user_name', 'read_exists'])
->withExists(['read' => function($q) use($post) {
return $q->where('post_id', $post->id);
}]
)->get();
It's stated in the docs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论