Laravel 5.0 Collection contains()无法按预期工作?

时间:2019-04-30 11:44:51

标签: php laravel laravel-5 eloquent

我有一段恋情。 1个邮件组有许多ExternalClients。

当我这样添加这个收藏集时:

dd($mailgroup->externalClients);

我得到这个结果:

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
    }
  ]
}

现在,我想像这样尝试$ collection-> contains()函数,我什么也没得到。

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

我希望看到“是”,因为我们可以看到“ mailtoganesh.p@gmail.com”在集合中。

我也尝试过:

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

这给了我这个错误:

  

类Closure的对象无法转换为int

3 个答案:

答案 0 :(得分:1)

您可以尝试where()收集助手功能。这将提供正确的结果。结果如下:-

$externalClients = $mailgroup->externalClients;
if($externalClients->where('email', 'mailtoganesh.p@gmail.com')->count() > 0) {
    echo "yes";
}

以下是集合帮助器功能的参考:where() docs

对于Laravel 5.0,您可以参考this

尝试一下并检查。我认为这会起作用。

答案 1 :(得分:0)

在修补匠中,我做了一些测试,看来where不是正确的方法。

>>> App\User::find(1)->roles->where('role_id', '<', 1)
=> Illuminate\Database\Eloquent\Collection {#3185
     all: [
       App\models\Role {#3176
         id: 1,
         name: "super-admin",
         display_name: "Super Admin",
         description: "This will be one permission, that can not be assigned or
modified.",
         created_at: "2018-12-13 12:09:07",
         updated_at: "2018-12-13 12:09:07",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3177
           user_id: 1,
           role_id: 1,
         },
       },
     ],
   }
>>>
Dell@DESKTOP-KU6707L MINGW64 /d/work/www/charmboard (master)
$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.10 — cli) by Justin Hileman
>>> App\User::find(1)->roles->where('role_id', 1)
=> Illuminate\Database\Eloquent\Collection {#3129
     all: [],
   }
>>> App\User::find(1)->roles->where('role_id', 1)->count()
=> 0
>>>

并为之工作

>>> App\models\Role::where('id', 1)->count()
=> 1

您的情况是第一个,我想,您可以急于加载和使用类似

>>> App\User::find(1)->roles()->where('role_id', 1)->get()
=> Illuminate\Database\Eloquent\Collection {#3138
     all: [
       App\models\Role {#3129
         id: 1,
         name: "super-admin",
         display_name: "Super Admin",
         description: "This will be one permission, that can not be assigned or
modified.",
         created_at: "2018-12-13 12:09:07",
         updated_at: "2018-12-13 12:09:07",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3128
           user_id: 1,
           role_id: 1,
         },
       },
     ],
   }
>>>

here前几天,我学到了这一课。

答案 2 :(得分:0)

您正在使用哪个版本的Laravel?

在v5.8中,我能够根据属性名称和值来过滤对象。

请参阅:

>>> $users = factory(App\User::class, 3)->make();
=> Illuminate\Database\Eloquent\Collection {#3278
     all: [
       App\User {#3276
         customer_id: 57,
         name: "Jeanie Cassin V",
         email: "ksporer@example.org",
         is_active: true,
         email_verified_at: DateTime @184931115 {#3273
           date: 1975-11-11 09:45:15.0 UTC (+00:00),
         },
         last_login_at: "2018-12-16 09:06:01",
       },
       App\User {#3285
         customer_id: 58,
         name: "Prof. Lula Moore",
         email: "gulgowski.brenden@example.net",
         is_active: true,
         email_verified_at: DateTime @270031087 {#3275
           date: 1978-07-23 08:38:07.0 UTC (+00:00),
         },
         last_login_at: "2007-02-19 00:27:52",
       },
       App\User {#3287
         customer_id: 59,
         name: "Conrad Hansen",
         email: "kenneth98@example.com",
         is_active: true,
         email_verified_at: DateTime @1026743001 {#3289
           date: 2002-07-15 14:23:21.0 UTC (+00:00),
         },
         last_login_at: "1987-10-06 18:45:35",
       },
     ],    }
>>> $rs = $users->contains('email', 'ksporer@example.org');
=> true
>>> $rs = $users->contains('email', 'other@example.com');
=> false
>>> $rs = $users->contains(function($user) { return $user->email === 'ksporer@example.org'; });
=> true
>>> $rs = $users->contains(function($user) { return $user->email === 'other@example.com'; });
=> false

但是如上所述,这已在Laravel 5.8中进行了测试。


您可以尝试使用匿名函数通过自定义比较获取正确/错误的返回值。

它将通过2个参数(在5.8中):

  1. 对象
  2. 集合中对象的键

根据您的情况,您可以跳过第二个参数。

例如:

$email = 'mailtoganesh.p@gmail.com';

$clientExists = $mailgroup->externalClients->contains(
    function (ExternalClient $externalClient) use ($email) {
        return $externalClient->email === $email;
    });

if ($clientExists) {
    // yes
}

如果需要集合中项目(对象)的键,则可以使用:

$result = $collection->contains(
    function ($object, $key) {
        // check something with $object->x or $key and return bool
    });