Home > Software design >  laravel collection returning object format when using where('field','!=',$value)
laravel collection returning object format when using where('field','!=',$value)

Time:07-19

   $details['series'] = $Details->where('id','!=',$id);

  it return object
{
    "1": {
        "id": 607,
        "title": "title",
        "audio": null,
        "thumb_image": null,    
    }
}

$details['series'] = $Details->where('id','==',$id);

[
    {
        "id": 606,
        "title": "title",
        "audio": null,
        "thumb_image": null,  
     
      
    }
]

why this happen, how to solve this issue

CodePudding user response:

Assuming that you are refering to what happens when the result is converted into JSON then the where (or any filter) retains the keys by default which when converted to a JSON string results in an object since arrays in JSON must be 0 based and not have any gaps within their keys.

You can reset the key numbering like so:

$details['series'] = $Details->where('id','!=',$id)->values();

This is like calling array_values on an array to achieve the same thing.

CodePudding user response:

Use a first() on your collection :

$details['series'] = $Details->where('id','!=',$id)->first();
  • Related