Laravel 5.0 Collection包含()无法正常工作



我有我的关系集合。1个邮件组有许多外部电量。

当我这样的收藏集时:

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';
}

这给了我这个错误:

类关闭的对象无法转换为int

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

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

这是收集辅助函数的参考:whese((文档

对于Laravel 5.0,您可以参考此。

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

在Tinker中我进行了一些测试,看来where不是正确的方法。

>>> AppUser::find(1)->roles->where('role_id', '<', 1)
=> IlluminateDatabaseEloquentCollection {#3185
     all: [
       AppmodelsRole {#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: IlluminateDatabaseEloquentRelationsPivot {#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
>>> AppUser::find(1)->roles->where('role_id', 1)
=> IlluminateDatabaseEloquentCollection {#3129
     all: [],
   }
>>> AppUser::find(1)->roles->where('role_id', 1)->count()
=> 0
>>>

并为

工作
>>> AppmodelsRole::where('id', 1)->count()
=> 1

我认为,您的情况是第一个1

>>> AppUser::find(1)->roles()->where('role_id', 1)->get()
=> IlluminateDatabaseEloquentCollection {#3138
     all: [
       AppmodelsRole {#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: IlluminateDatabaseEloquentRelationsPivot {#3128
           user_id: 1,
           role_id: 1,
         },
       },
     ],
   }
>>>

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

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

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

请参阅:

>>> $users = factory(AppUser::class, 3)->make();
=> IlluminateDatabaseEloquentCollection {#3278
     all: [
       AppUser {#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",
       },
       AppUser {#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",
       },
       AppUser {#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
    });

最新更新