首页 > 解决方案 > 我想获取为特定链接投票的用户列表

问题描述

我想显示投票给特定链接的用户列表,例如投票给链接 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();
        });
    }

标签: phplaravel

解决方案


您是否专门尝试从 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);

推荐阅读