Laravel集合返回值

时间:2017-06-16 12:18:15

标签: php laravel collections

我在Laravel 5.4中有这个方法:

/**
     * Check if domain is valid
     *
     * @param string $emails
     * @param string $domain
     * @return bool
     */
    public function isValidDomain(string $emails, string $domain)
    {
        collect(explode(',', $emails))->each(function($email) use ($domain) {
            return $this->containsAtSymbol($email) && $this->getDomain($email) == $domain;
        });
    }

在这里,我检查来自电子邮件地址的域名是否有效。那么我怎么能说当其中一个是真的时候,那么返回true:

return $this->containsAtSymbol($email) && $this->getDomain($email) == $domain;

1 个答案:

答案 0 :(得分:1)

使用方法contains,如下所示:

$result = collect(explode(',', $emails))->contains(function($email) use ($domain) {
   return $this->containsAtSymbol($email) && $this->getDomain($email) == $domain;
});

$result值为truefalse