samedi 7 septembre 2019

Laravel sortBy() based on string values

In my Laravel application, a shirt has many sizes:

public function sizes()
{
    return $this->hasMany(\App\Size::class, 'size_id');
}

A size has an attribute called name that can be SM, MD, LG, XL, XXL, etc.

I would like to append a sortBy() to the eloquent relationship, so that sizes always appear in order from SM up to XXL.

Is it possible to write a sort based on string values? I've only ever written them based on whether one value was greater than another, but that obviously doesn't apply here.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire