PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 26, 2022

[FIXED] OrderBy Coupons based on expiration status Laravel

 January 26, 2022     eloquent, laravel, laravel-5, mysql, php     No comments   

Issue

I need to display list of coupons and order it based on created_at date and expiration status.

The columns of my Coupons table

id
name
price
expired_at
created_at
status

My laravel query

$coupons= Coupon::where('status', 2)->orderBy('created_at', 'DESC')->orderBy( First I will need to compare expired_at with today date here)->get();

The problem is I dont know how to orderBy coupon_status, I want to display all expired coupons at bottom of the list and on-going coupons at top of the list and also orderBy created_at (the newest and on-going coupons is at the top of the list)

so probably need to create temporary column to get expiration status, before to order the list. Any Idea how to achieve this?


Solution

compare expired at with today (expired_at < NOW())

$coupons= Coupon::where('status', 2)->orderByRaw(
 "CASE WHEN expired_at IS NULL OR expired_at > CURDATE() THEN 1 ELSE 0 END DESC"
);


Answered By - Ali Ghaini
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing