简体   繁体   English

Laravel WithCount 使用 where

[英]Laravel WithCount using where

i have 3 tables:我有 3 张桌子:

  1. Table 1 courses :表 1 courses

    • id_cours id_cours
    • field_cours field_cours
  2. Table 2 Question :表 2 Question

    • id_quest id_quest
    • field_quest field_quest
    • id_cours id_cours
    • id_user id_user
  3. Table 3 Users : -id -nom表 3 Users :-id -nom

The question is i want to Count all question just added by the user logged.问题是我想统计用户刚刚添加的所有问题。

i try with this one in the Model Courses: but i want to add where id_user = id我尝试在 Model 课程中使用这个:但我想添加 where id_user = id

 public function answers()
    {
        return $this->hasMany('App\QuestionUsers','idcours', 'id_cours');

    }

but this returns count of all question added by all users.但这会返回所有用户添加的所有问题的计数。 Please need Help请需要帮助

i found solution just i call the Auth and add where我找到了解决方案,只是我调用了 Auth 并添加了 where


 public function answers()
    {
        return $this->hasMany('App\QuestionUsers','idcours', 'id_cours')->where('idusers',Auth::id());

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM