dimanche 16 décembre 2018

Sorting a field based on several values in Laravel

I have an array of city codes

$cities=[9,12,14,2,3,4,5,6,8,10,11,13]

My posts table has a foreign key named city_id I want sorting posts based on the values of this array In this way: the first posts of the city 9 then the posts of the city 12 and then posts of city 14 and etc to be loaded

I tried using this method but this is wrong

$posts->orderByRaw('city_id in ? desc',$cities);

Can you help me find the best solution?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire