I have a Accessor method in Collection Model getSizesAttribute, which returns array of available sizes eg: [‘S’,’L’], Now I need to get Models with have size ‘S’. like:

$collections = $collections->where('sizes','S');

But sizes is array, could I manipulate this anyhow so that I could check returns only if sizes have specific size.

I tried making another method getIsSizeAttribute, like:

public function getIsSizeAttribute($size){
      return in_array($size,$this->sizes);
}

Now How could I user this in Where condition like

$collections = $collections->where('is_size','S');

Solution

You should use filter on collection

$collections = $collections->filter(function($item, $index) {
    return in_array('S', $item->sizes);
});


Continue Reading...

I have a Accessor method in Collection Model getSizesAttribute, which returns array of available sizes eg: , Now I need to get Models with...

Read Post

I have a Accessor method in Collection Model getSizesAttribute, which returns array of available sizes eg: , Now I need to get Models with...

Read Post