2016-11-12 1 views
1

が含まれています。https://laravel.com/docs/5.3/collections#method-containsには、ララベルメソッドが使用されています。しかし、それは私のためには機能しません。ララベルコレクションには

foreach ($this->options as $option) { 
    if($options->contains($option->id)) { 
     dd('test'); 
    } 
} 

dd($options);次のようになります。dd($option->id);

Collection {#390 
    #items: array:1 [ 
    0 => array:3 [ 
     0 => array:7 [ 
     "id" => 10 
     "slug" => "test" 
     "name" => "test" 
     "poll_id" => 4 
     "created_at" => "2016-11-12 20:42:42" 
     "updated_at" => "2016-11-12 20:42:42" 
     "votes" => [] 
     ] 
     1 => array:7 [ 
     "id" => 11 
     "slug" => "test-1" 
     "name" => "test" 
     "poll_id" => 4 
     "created_at" => "2016-11-12 20:42:42" 
     "updated_at" => "2016-11-12 20:42:42" 
     "votes" => [] 
     ] 
     2 => array:7 [ 
     "id" => 12 
     "slug" => "test-2" 
     "name" => "test" 
     "poll_id" => 4 
     "created_at" => "2016-11-12 20:42:42" 
     "updated_at" => "2016-11-12 20:42:42" 
     "votes" => [] 
     ] 
    ] 
    ] 
} 

結果が10です。

何が間違っている可能性がありますか?それとも良い方法がありますか?

+0

コードはテキストとして投稿してください。画像はありません。 – Blag

+0

@Blagはそれを変更しました! – Jamie

答えて

6

containsメソッドに の指定されたペアが存在するかどうかを判定するために、キーと値のペアを渡す必要があります。

あなたは、このようにcontains()メソッドを使用する必要があります。

foreach ($this->options as $option) { 
    // Pass key inside contains method 
    if($option->contains('id', $option->id)) { 
     dd('test'); 
    } 
} 

ホープこれは

1
foreach ($this->options as $option) { 
    if(!$options->flatten(1)->where('id',$option->id)->isEmpty()) { 
     dd('test'); 
    } 
} 
1

使用Laravelを伝え、以下を助け、あなたが 'ID' を合わせたい:

$options->contains('id', $option->id); 

Docs

関連する問題