douwu5009 2018-10-18 18:52
浏览 504
已采纳

“PHP错误:从Laravel Collection上的diff()方法调用字符串上的成员函数getKey()

Here are the dumped collections:

//$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}"
  ]
}

Here is the error:

{
    "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": "->"
        },

Here is the line 174 referenced in the above error:

$diff = $collOne->diff($collTwo);

The docs seem pretty straight forward:

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:

Am I missing something obvious here?

  • 写回答

1条回答 默认 最新

  • douyuben9434 2018-10-18 20:01
    关注

    I suspect when you called mapWithKeys you did not return a Model instance. That's what's causing the error. Illuminate\Database\Eloquent\Collection must be a collection of Eloquent models but in this case it isn't.

    A workaround could be calling diff in the base collection rather than the Eloquent collection like so:

    $diff = $collOne->toBase()->diff($collTwo->toBase());
    

    Or alternatively, when you call mapWithKeys, call it on the base collection like so:

    $eloqCollection->toBase()->mapWithKeys(function($...) {...}) for both the collections. Then you can use the same code: $diff = $collOne->diff($collTwo); to compute the diff because now both $collOne and $collTwo would be instances of Illuminate\Support\Collection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line