php - 我想获取为特定链接投票的用户列表
问题描述
我想显示投票给特定链接的用户列表,例如投票给链接 1..etc 的用户列表我得到每个链接的投票数,但我不知道如何获取用户。
链接型号:
/**
*
*links votes with respect to users
*@return
*/
public function votes(){
return $this->hasMany(Communitylinkvotes::class,'community_links_id');
}
// practise
public function userss(){
return $this->hasMany(Communitylinkvotes::class,'user_id','community_links_id');
}
CommunityLinkVotes 模型或数据透视表模型
class Communitylinkvotes extends Model
{
//
protected $table = 'community_links_votes';
}
为选票迁移
public function up()
{
Schema::create('community_links_votes', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->index();
$table->integer('community_links_id')->index();
$table->timestamps();
});
}
解决方案
您是否专门尝试从 Link 模型开始获取用户列表?
如果没有,请执行以下操作:
User::whereHas('votes', function($query) use ($some_id) {
$query->where('community_links_id', $some_id);
})->get();
如果是这样,我认为您在 Link 模型上的用户关系需要是hasManyThrough
. 就像是:
$this->hasManyThrough(User::class,Communitylinkvotes::class);
推荐阅读
- php - 将字符串中的数字单词转换为数字
- security - 在 mac 上安装 mobsf 时出错
- templates - SAPUI5 预处理绑定
- java - 如何使用 Appium 在 Android Studio for java 中为除 First Activity 之外的 Activity 编写自动化测试
- javascript - 流默认值类型检查
- r - R - Aggregating and returning value that minimises another dimension
- sql - 加入并计算出现次数
- c# - 如果命令行开关遇到错误,如何终止脚本执行
- java - 设置阿尔巴尼亚语/黑山语语言环境时,它会恢复为默认值
- django - Beanstalk 没有运行 makemigrations