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 ?
what is RESPONDED. ?
what does that magic number 1 represent?
@Snapey Listings model
public function responses(){
return $this->hasMany(ListingResponses::class,'listing_id','id')->sum('user_id');
}
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);
@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.
@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.
@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?
why did you not use responses?
@Snapey My guess is that it breaks due to its an aggregate ->sum('user_id');
oh i see, because responses is not a relationship it's an aggregate
you need to fix that
@Snapey Yes. I fixed then model. Thanks.
public function response(){
return $this->belongsTo(ListingResponses::class,'dealrealtor_id','user_id');
}
But still its not working
@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.
@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
@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 :)
@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 sign in or create an account to participate in this conversation.