"PHP Error: Call to a member function getKey() on string" from diff() method on Laravel Collection
"PHP Error: Call to a member function getKey() on string" from diff() method on Laravel Collection
以下是转储的集合:
//$collOne
Illuminate\Database\Eloquent\Collection {#3386
#items: array:6 [
4807 => "{"color_id":7,"size_id":4,"pack_id":null}"
4808 => "{"color_id":7,"size_id":2,"pack_id":null}"
4809 => "{"color_id":7,"size_id":6,"pack_id":null}"
4840 => "{"color_id":44,"size_id":4,"pack_id":null}"
4841 => "{"color_id":44,"size_id":6,"pack_id":null}"
4842 => "{"color_id":44,"size_id":2,"pack_id":null}"
]
}
//$collTwo
Illuminate\Database\Eloquent\Collection {#3403
#items: array:5 [
430 => "{"color_id":7,"size_id":4,"pack_id":null}"
431 => "{"color_id":7,"size_id":2,"pack_id":null}"
433 => "{"color_id":44,"size_id":4,"pack_id":null}"
434 => "{"color_id":44,"size_id":6,"pack_id":null}"
435 => "{"color_id":44,"size_id":2,"pack_id":null}"
]
}
这是错误:
{
"message": "Call to a member function getKey() on string",
"exception": "Symfony\Component\Debug\Exception\FatalThrowableError",
"file": "[obfuscated]/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php",
"line": 281,
"trace": [
{
"file": "[obfuscated].php",
"line": 174,
"function": "diff",
"class": "Illuminate\Database\Eloquent\Collection",
"type": "->"
},
这是上述错误中引用的第 174 行:
$diff = $collOne->diff($collTwo);
文档看起来很简单:
The diff method compares the collection against another collection or
a plain PHP array based on its values. This method will return the
values in the original collection that are not present in the given
collection:
我是不是漏掉了什么明显的东西?
我怀疑当您调用 mapWithKeys
时您没有 return Model
实例。这就是导致错误的原因。 Illuminate\Database\Eloquent\Collection
必须是 Eloquent 个模型的集合,但在本例中不是。
解决方法是在基础集合中调用 diff,而不是像这样在 Eloquent 集合中调用:
$diff = $collOne->toBase()->diff($collTwo->toBase());
或者,当您调用 mapWithKeys
时,像这样在基础集合上调用它:
$eloqCollection->toBase()->mapWithKeys(function($...) {...})
两个系列。然后您可以使用相同的代码:$diff = $collOne->diff($collTwo);
来计算差异,因为现在 $collOne
和 $collTwo
都是 Illuminate\Support\Collection
的实例
以下是转储的集合:
//$collOne
Illuminate\Database\Eloquent\Collection {#3386
#items: array:6 [
4807 => "{"color_id":7,"size_id":4,"pack_id":null}"
4808 => "{"color_id":7,"size_id":2,"pack_id":null}"
4809 => "{"color_id":7,"size_id":6,"pack_id":null}"
4840 => "{"color_id":44,"size_id":4,"pack_id":null}"
4841 => "{"color_id":44,"size_id":6,"pack_id":null}"
4842 => "{"color_id":44,"size_id":2,"pack_id":null}"
]
}
//$collTwo
Illuminate\Database\Eloquent\Collection {#3403
#items: array:5 [
430 => "{"color_id":7,"size_id":4,"pack_id":null}"
431 => "{"color_id":7,"size_id":2,"pack_id":null}"
433 => "{"color_id":44,"size_id":4,"pack_id":null}"
434 => "{"color_id":44,"size_id":6,"pack_id":null}"
435 => "{"color_id":44,"size_id":2,"pack_id":null}"
]
}
这是错误:
{
"message": "Call to a member function getKey() on string",
"exception": "Symfony\Component\Debug\Exception\FatalThrowableError",
"file": "[obfuscated]/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php",
"line": 281,
"trace": [
{
"file": "[obfuscated].php",
"line": 174,
"function": "diff",
"class": "Illuminate\Database\Eloquent\Collection",
"type": "->"
},
这是上述错误中引用的第 174 行:
$diff = $collOne->diff($collTwo);
文档看起来很简单:
The diff method compares the collection against another collection or a plain PHP array based on its values. This method will return the values in the original collection that are not present in the given collection:
我是不是漏掉了什么明显的东西?
我怀疑当您调用 mapWithKeys
时您没有 return Model
实例。这就是导致错误的原因。 Illuminate\Database\Eloquent\Collection
必须是 Eloquent 个模型的集合,但在本例中不是。
解决方法是在基础集合中调用 diff,而不是像这样在 Eloquent 集合中调用:
$diff = $collOne->toBase()->diff($collTwo->toBase());
或者,当您调用 mapWithKeys
时,像这样在基础集合上调用它:
$eloqCollection->toBase()->mapWithKeys(function($...) {...})
两个系列。然后您可以使用相同的代码:$diff = $collOne->diff($collTwo);
来计算差异,因为现在 $collOne
和 $collTwo
都是 Illuminate\Support\Collection