2017-02-11 3 views
1

のlaravel配列からidを取得して、1つの配列から複数のIdをフェッチしようとしています。 これは私が試したものです:複数のIDの

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

私はこのエラーを取得する:

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

私は$以下をログインした場合、私は4人の異なるユーザーを以下としています見ることができるよう、私は、以下の配列を取得私はそれらのIDを取得する必要があります:

local.INFO: [{"id":32,"email":"[email protected]","username":"[email protected]","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":"[email protected]","username":"[email protected]","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":"[email protected]","username":"[email protected]","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":"[email protected]","username":"[email protected]","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) 

答えて

0

コレクションから複数の列を取得する(例: followingコレクションの複数のID)は、次のように使用してください:

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

完璧!ありがとう –

関連する問題