- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 leftJoin 进行 laravel select 时遇到问题。我试图选择 2 个帖子并计算有多少条评论(第一个帖子有 7 个评论,第二个 - 0),但我只得到第一个帖子有 7 个评论。
代码是:
$posts = DB::table('posts')
->leftJoin('comments', 'comments.post', '=', 'posts.id')
->select(DB::raw('posts.title, posts.body, posts.created_at, posts.slug, CASE WHEN comments.post IS NULL THEN 0 WHEN comments.post IS NOT NULL THEN count(comments.post) END as count'))
->get();
当我尝试检查我在网络浏览器中看到的内容时,出现错误:调用非对象上的成员函数 count()
我的 View 文件中使用 @if($posts->count()) 的行出现此错误
我已经调试过,使用 print_r()
只得到了一篇文章。
有什么建议吗?
最佳答案
我认为最好的选择是使用 laravel 的 Eloquent ORM 的一些内置功能。
在模型中建立关系:
Post.php:
<?php
class Post extends Eloquent {
protected $table = 'posts';
public function comments()
{
return $this->hasMany('Comment', 'posts');//first param refrences the other model, second is the foreign key
}
Comment.php:
<?php
class Comment extends Eloquent {
protected $table = 'comments';
public function comments()
{
return $this->belongsTo('Post');//first param refrences the other model, second is unnecessary if you are using auto incrementing id
}
现在您已经建立了关系,无需加入。
用法:
可能有更好的方法来做到这一点,但这应该可行。
$posts = Post::with('comments')->get();//retrieves all posts with comments
foreach($posts as $post){
$count = count($post['comments']);
$post['comment_count'] = $count;
}
return $posts;
这将返回一个包含所有帖子的结果,其中有一个名为“comments”的字段,其中包含所有相关评论的数组。 “comment_count”字段将包含计数。
示例:
[
{
"id": 1,
"created_at": "2014-07-02 11:34:00",
"updated_at": "2014-07-02 11:34:00",
"post_title": "hello there",
"comment_count": 1,
"comments": [
{
"id":'blah'
"comment_title":"blah"
}
]
}
您现在可以将其传递到您的 View 并循环浏览每个帖子并获取 $post['comment_count']
关于php - Laravel leftJoin 未正确返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24558648/
代码在 EventUserTypes 模型中 $this->find() ->select(['event_usertypes.user_type_id' , 'usertypes.n
我的 eloquent 模型有一些问题,我尝试将两个不同的表合并为一个 现在我有两个表。 第一个表名称“company_saved_cv” |id|company_id|worker_id | ---
我正在尝试将 leftJoin 添加到 hasOne 方法中,但它似乎不起作用。来自主表的数据存在,但不来自连接表。代码: public function getUser() {
我是 laravel 的新手。 我有两张 table 。1)产品2) 价格 ----------------------------- - products - --
此代码返回“count”和“odds”值完全相同(它们不应该相同)。它实际上只计算“o.id”并为两者返回相同的值。 如何正确计算“b.id”? \DB::table('matches as
我有这个 SQL 代码,我想把它转换成 ormlite - 但我不知道如何以最好的方式做到这一点。 SELECT * FROM Job INNER JOIN Emp ON Job.JobAnsvar
我在使用 leftJoin 进行 laravel select 时遇到问题。我试图选择 2 个帖子并计算有多少条评论(第一个帖子有 7 个评论,第二个 - 0),但我只得到第一个帖子有 7 个评论。
我是 Laravel 的新手,正在学习 leftJoin。 我有一个名为“users”的表,其中行为“steam_id”。 我有另一个名为“匹配”的表。 “匹配”表有一个名为的唯一行“match_
我正在尝试使用此 Controller 删除“ super 管理员”: $user->superadmin = DB::table('role_users AS ru') ->leftJoin('us
这个问题在这里已经有了答案: Join between tables in two different databases? (4 个答案) 关闭 6 年前。 我有一个项目,其中有两个 MySQL
我在 MySQL 中有两个表。 logs 表包含列:time、user_id、event_type users 表有列:email, id 每次用户登录到生成日志文件的系统时,event_type 列
我的数据库中有以下表格: 连接 编号 owner_id 所有者 编号 名 ConnectionsController.php $connections = DB::table('connections
我有一个问题 Select m.name from machines m innerJoin config c ON m.cod = c.cod --> where ( c.categ
在我的 laravel 5.7/eloquent/Mysql 5.5 应用程序中,我需要在聊天列表中获取任何聊天消息的值以及给定 useronly ($user_id) 的任何聊天消息的另外 1 列消
我在类客户和类产品之间有很多关系。例如 数据库客户 | ID | Name | ----------------------- | 01 | Jack | | 02 | Joe | |
如何将以下查询转换为 Doctrine: SELECT restaurants.restaurant_name , restaurants.restaurant_id,
表 1) m_conservationsetting +------------+-------+------------+ | FacilityId | Unit | CategoryId | +
我有一个产品,其描述、名称等以多种语言保存在另一个表中。一种产品可以有多种翻译(产品数据)。 目前,我获得了产品的所有翻译,并执行了 foreach 和 ifs 来根据语言环境获得所需的翻译...如果
我有 3 个模型: 用户 喜欢 发帖 Like 在 user_id 和 post_id(这是其他 2 个模型的 id 列)上使用 CakePHP 约定设置了 2 个外键。 我想运行一个查找,在 Pos
旧的 leftJoin 问题的一个细微变化:我有两个要加入的表: ID Val1 1 100 2 200 3 300 和 ID Val2 2 50 已关注 combine two tables
我是一名优秀的程序员,十分优秀!