mardi 30 avril 2019

Laravel 5.0 Collection contains() not working as expected?

I have a collection of my relationship. 1 Mailgroup has many ExternalClients.

When I dd this collection like this:

dd($mailgroup->externalClients);

I get this result:

Collection {#304 ▼
  #items: array:1 [▼
    0 => ExternalClient {#303 ▼
      #table: "external_clients"
      +timestamps: false
      #casts: array:1 [▶]
      #fillable: array:7 [▶]
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      #attributes: array:8 [▼
        "id" => 1
        "firstname" => "Ganesan "
        "lastname" => "Pandaram"
        "email" => "mailtoganesh.p@gmail.com"
        "active" => 1
        "lang" => "nl"
        "company" => "ypto"
        "site_id" => 4
      ]
      #original: array:10 [▶]
      #relations: array:1 [▶]
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
    }
  ]
}

Now I want I try the $collection->contains() function like this I get nothing.

if ($mailgroup->externalClients->contains('mailtoganesh.p@gmail.com')) {
    echo 'yes';
}

I am expecting to see 'yes' because we can see that the "mailtoganesh.p@gmail.com" is in the collection.

I've also tried this:

if ($mailgroup->externalClients->contains('email', 'mailtoganesh.p@gmail.com')) {
    echo 'yes';
}

This gives me this error:

Object of class Closure could not be converted to int



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire