【问题标题】:Checking if ID exists in collection containing multiple key values检查包含多个键值的集合中是否存在 ID
【发布时间】:2016-05-20 02:11:04
【问题描述】:

我正在使用 Laravel 5 创建一个 API 并致力于以下用户功能。

我有一个包含 user_id 和 user_following_id 的数据透视表。当对用户发出 GET 请求时,我会建立一个关系以返回与 user_id 匹配的所有行。

public function followers()
{
    return $this->hasMany('App\Follower')->select(array('project_id', 'user_id'));
}

在我的脑海中,我的目标只是检查用户 ID(发出请求的人)是否存在于从关系返回的集合中。但是,为了使关系起作用,它会返回两个关键值:

"followers":[{"project_id":23,"user_id":6}]}]

有没有更好的方法来做我想做的事?

我只想检查请求中的 ID 是否存在于 user_id 匹配的数据透视表中。

【问题讨论】:

    标签: php api laravel-5 relationship


    【解决方案1】:

    不会

    public function followers(\Request $request) {
        return $this->hasMany('App\Follower')->select(array('project_id', 'user_id'))->where(['user_id', $request->user_id]);
    }
    

    会更容易吗?

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多