lundi 30 novembre 2020

Laravel how to sort collection by specific value and id

How to sort collections by specific name and id. I need to sort records where the name = X should be the first in the order and the rest should be sort by id.

product model

|---------------------|------------------|
|          id         |        Name      |
|---------------------|------------------|
|          1          |         a        |
|---------------------|------------------|
|          2          |         x        |
|---------------------|------------------|
|          3          |         b        |
|---------------------|------------------|
|          4          |         c        |
|---------------------|------------------|
|          5          |         x        |
|---------------------|------------------|
|          6          |         z        |
|---------------------|------------------|

I tried this code but not working.

Product::orderBy('id', 'asc')->get()


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire