Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FounderStartup's avatar

list properties ordered by featured...?

I need to show listings sorted by the responses_count but need featured listings on the top. 'featured' is a field in the listings table with a date of expiry of the featured facility. So need to consider featured only if the expiry date is greater than the present date.

Controller

      $listingsforsale = Listings::where('project_id', '<>', NULL)
        ->where('expiry_date','>' , now())
        ->where('listingtype', 1)
        ->where('dealstatus',1)
        ->where('status',1)
        ->withCount('response')
        ->orderBy('response_count', 'desc')
        ->orderBy('featured', 'DESC')->latest()
        ->paginate(10);

How to do that ?

0 likes
16 replies
Sinnbeck's avatar

You will need to order by raw and manually write a case statement https://stackoverflow.com/a/39941031

But wouldn't it be easier to just remove the featured flag when the expiry date is lesser than the present date. (scheduled job)?

1 like
FounderStartup's avatar

@Sinnbeck Thanks.

Or shall I create a ne model just for featured ? with expiry date ? Will it work to will it make more complicated ?

Sinnbeck's avatar

@FounderStartup probably about the same. If it was on a different table I might try a left join on that table and restrict it to expired_at in the future (meaning it would be null otherwise). But it will still require some "custom" sql

1 like
Sinnbeck's avatar

But setting them as no longer being featured isn't a solution?

1 like
FounderStartup's avatar

@Sinnbeck Yes chief... :) that will be a better idea. Can you share a link on how to use a scheduler for this type of work ? :)

FounderStartup's avatar

@Sinnbeck

Just need a minor clarification :

What I have understood is that

  1. I need to create a class with artisan command like php artisan make:command sendWeeklyReport

This will have then code for the required functionality.

  1. Then add code in App\Console\Kernal.php.

To run on the local : the command is - php artisan schedule:work

Is it correct ? How will I run this on production server ? Do I need to create a cron ?

Sinnbeck's avatar

@FounderStartup That can be use for sending the weekly report. My suggestion was to also make a command RemoveFeaturedFromExpiredListings that is run every hour :)

But locally you can use schedule:work, but on production you need to set up cron :)

1 like
FounderStartup's avatar

@Sinnbeck Hey chief. Need a bit of help here.

I have configured the scheduler and its working fine on the local. What exactly I need to do on the production server ?

Do I need to run the command php artisan schedule:work through crone or what to do ?

Sinnbeck's avatar

@FounderStartup On production you use schedule:run in a cron job :)

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
1 like
Sinnbeck's avatar

@FounderStartup Not quite

  • run = check what needs to be run and exit afterwards
  • work = keep running forever but check every minute (cannot be used with cron)
1 like

Please or to participate in this conversation.