I have a Products table
products: id, title, stock, price
I want new products to be at top of the list (first page), but I also want products with 0 stock to be at the end of the list (last page)
obviously, this doesn't work the way I want it, a new product with 0 stock still will show up first
Product::orderBy('id' , 'desc')->orderBy('stock' , 'desc')->paginate(20)
there are lots of similar questions but all seem to be about null values which don't work for me .. also I tried
->orderByRaw('stock = 0', 'ASC', 'id' , 'desc' )->paginate(20)
as was suggested but I get an empty collection
it's a pagination query, sort has to happen in the database, and sorting the collection after reading them from DB won't work
------------------------------------ EDIT ------------------------------------
i tried this code
Product::orderByRaw('stock = 0', 'ASC')
->orderBy('id' , 'desc')
->get()
which will generate this query in debugger
select * from `products` order by stat_view = 0, `id` desc
this code will work when i try it directly in database , but for some reason in laravel i get an empty collection .... this is an old website which is using laravel 5.2 not sure if that has anything to do with the problem
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3dIYoI7
via IFTTT
Aucun commentaire:
Enregistrer un commentaire