8

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?

9
  • 1
    Can you paste the entire stacktrace? Also, it would be interesting to understand how you got to the Eloquent collections - through a query or manually created? Commented Oct 18, 2018 at 19:23
  • The collections were formed with a mapWithKeys call on eloquent collections where I returned only specific properties and json encoded them in order to make comparisons on the two collections. Commented Oct 18, 2018 at 19:28
  • 1
    oh dang, lol. It's the wheelmaker! small world eh? Commented Oct 18, 2018 at 19:29
  • haha, refresh my memory... did I get into a late night argument with you on something? I've been working on a project non-stop, 10+ hours a day for too many months to count now. Commented Oct 18, 2018 at 19:32
  • Yea lol, techies I tell ya. Argue about everything from frameworks to approach to the "best" code :D Commented Oct 18, 2018 at 19:33

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

nice catch @Paras

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.