从具有多个id的laravel数组中获取id

时间:2017-02-11 21:26:21

标签: php arrays laravel

我试图从一个数组中获取多个Id,然后在查询关系中使用它。 这就是我试过的:

    $following = $user->following;
    $followingId = $following->id;

我收到此错误:

'Undefined property: Illuminate\Database\Eloquent\Collection::$id'

如果我记录下面的$,我会得到以下数组,因为你可以看到我关注4个不同的用户,我需要得到这些ID:

local.INFO: [{"id":32,"email":"test@mail.com","username":"test@mail.com","photo":"97dfebf4098c0f5c16bca61e2b76c373","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":1,"pivot":{"follower_id":1,"followed_id":32}},{"id":69,"email":"fhamada@paginar.net","username":"fhamada@paginar.net","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":69}},{"id":79,"email":"sandrastun@gmail.com","username":"sandrastun@gmail.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":79}},{"id":101,"email":"cristobalalfonzo@outlook.com","username":"cristobalalfonzo@outlook.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":101}}]  

查询关系:

$activities = DB::table('notifications')
                ->join('users', 'users.id', '=', 'notifications.sender_id')
                ->join('followers', 'followed_id', '=', 'users.id')
                ->join('user_details', 'user_details.id', '=', 'notifications.user_id')
                ->join('winwins', 'winwins.id', '=', 'object_id')
                ->leftJoin('groups', 'notifications.object_id', '=', 'groups.id')
            ->where('notifications.sender_id', '=', $user->id)
            ->orWhere('winwins.id', '=', 'notifications.object_id')
            ->orWhere('sender_id', '=', $user->id)
            ->orWhere('followers.followed_id', '=', $followingId)
            ->orderBy('sent_at', 'desc')
            ->skip($page * $amount)
            ->take($amount)

1 个答案:

答案 0 :(得分:0)

要从集合中提取多个列(例如,following集合中的多个ID),请使用以下命令:

$followingId = $following->pluck('id')->toArray();