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

FounderStartup's avatar

Query to show listing with responds on top ?

Need to show listings with RESPONDED listings on the top.

$openlistings = Listings::where('expiry_date','>' , now())->where('dealstatus', 1)->where('status', '1')->where('user_id',  $id)->paginate(10);

What will be the correct query ?

0 likes
18 replies
Snapey's avatar

what is RESPONDED. ?

what does that magic number 1 represent?

1 like
FounderStartup's avatar

@Snapey Listings model

    public function responses(){
    	return $this->hasMany(ListingResponses::class,'listing_id','id')->sum('user_id');
    }
Snapey's avatar

you could try withCount but i'm not sure if it will be relevant to the sort

$openlistings = Listings::query()
        ->where('expiry_date','>' , now())
        ->where('dealstatus', 1)
        ->where('status', '1')
        ->where('user_id',  $id)
        ->withCount('responses')
        ->orderByDesc('responses_count')
        ->paginate(10);
1 like
FounderStartup's avatar

@Snapey

That was giving error.

I added a model 'response' as

    public function response(){
    	return $this->belongsTo(ListingResponses::class,'dealrealtor_id','user_id');
    }

But not getting the desired result.

FounderStartup's avatar

@Sinnbeck

    $openlistings = Listings::query()
        ->where('expiry_date','>' , now())
        ->where('dealstatus', 1)
        ->where('status', '1')
        ->where('user_id',  $id)
        ->withCount('response')
        ->orderBy('response_count', 'desc')
        ->paginate(10);

Listings are not sorted on the number of responses.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@FounderStartup That sounds really weird. Can you see what they are sorted by? If you show the response_count column in the output, does that show the right number or just 0 for all of them?

1 like
Sinnbeck's avatar

@Snapey My guess is that it breaks due to its an aggregate ->sum('user_id');

1 like
Snapey's avatar

oh i see, because responses is not a relationship it's an aggregate

you need to fix that

1 like
FounderStartup's avatar
  public function response(){
    	return $this->belongsTo(ListingResponses::class,'dealrealtor_id','user_id');
    }

But still its not working

Sinnbeck's avatar

@FounderStartup Remember that we cannot see your computer. Saying "its not working" gives us nothing new to work with. I asked some questions above to help you debug the problem.

1 like
FounderStartup's avatar

@Sinnbeck Yes chief I tried to answer but laracast did not allowed so fast answers :).

Finally I debugged it. As suggested by you ....

  public function response(){
    	return $this->hasMany(ListingResponses::class,'listing_id','id');
    }

Thanks

Sinnbeck's avatar

@FounderStartup I asked if you could check what count it is actually getting. If it is getting 0 or 1 for every row it wont be sorted :)

1 like
FounderStartup's avatar

@Sinnbeck Yes I checked it chief :) Thanks. Actually I got into digital marketing for the last whole month. Coding after a gap .... so feeling a bit lost. Or may be you need a different mind for marketing and coding :).

Please or to participate in this conversation.