Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Can I add a new property to resultset with array?

I have a resultset that return from paginator


 $Products = $paginator->getPaginate();

I am trying to assgin a new property for each of Product, but it is not working, I wondering is there a way to make it work?


    $imageList = array(
                'imagename1.jpg',
                'imagename2.jpg',
        );

    foreach($Products->items as $p){
        $p->imageList = $imageList;
    }

currently $p->imageList is not working.



33.7k
Accepted
answer

Resultsets are inmutable, you have to create an array for that:

$imageList = array(
        'imagename1.jpg',
        'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
        $p->imageList = $imageList;
        $items[] = $p;
}


6.7k

thanks

Resultsets are inmutable, you have to create an array for that:

$imageList = array(
       'imagename1.jpg',
       'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
       $p->imageList = $imageList;
      $items[] = $p;
}


6.7k

It's actually works now with

$imageList = array(
        'imagename1.jpg',
        'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
        $p->imageList = (array) $imageList;
        $items[] = $p;
}

$Products->items = $items;

where I have to specify with (array) $imageList;

Resultsets are inmutable, you have to create an array for that:

$imageList = array(
       'imagename1.jpg',
       'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
       $p->imageList = $imageList;
      $items[] = $p;
}