nested query issue I need '$similarlistings' with city = $listing->city. Its a simple query but I may be missing some basics here :)
controller
public function ListingDetails($id)
{
$listing = Listing::findOrFail($id);
Listing::find($id)->increment('views');
$responses = ListingResponse::where('listing_id', $id)->paginate(10);
$totalresponses = ListingResponse::where('listing_id', $id)->count();
$similarlistings = Listing::with('cityname','listingpropertytypename','listingtypename')->take(2)->get();
return view('frontend.listing-details', compact('similarlistings','listing', 'responses', 'totalresponses'));
}
You give it an array with a function. You can you both examples together if want to filter both
Listing::with(['cityname' => function ($q) use ($listing) {
$q->where('name', $listing->city);
}])
//or filter listings
Listing::whereHas('cityname', function ($q) use ($listing) {
$q->where('name', $listing->city);
})
@Sinnbeck Thanks . And what if I need to add another query ? like I need "$similarlistings" with city = $listing->city and propertytype = $listing->propertytype ? Need to chain it or can be used in same bracket ?
@FounderStartup either. Personally I would make one for each, but both works
Listing::with(['cityname' => function ($q) use ($listing) {
$q->where('name', $listing->city);
}])->with('listingpropertytypename','listingtypename')
@Sinnbeck
$similarlistings = Listing::with(['cityname' => function ($q) use ($listing) {
$q->where('id', $listing->city);
}])
->with(['listingpropertytypename' => function ($q) use ($listing) {
$q->where('id', $listing->property_type);
}])
->with('cityname','listingpropertytypename','listingtypename')->take(2)->get();
But I am not getting the required result ? what I am missing here ?
@FounderStartup I assume you want me to guess what you want and what you are getting? I then guess that you need to add a whereHas also. That will filter the listings. It's in my first post
What you have now, filters the relationship, and whereHas filters the main query
@Sinnbeck :)
I just want a query to get
"$similarlistings" with city = $listing->city and propertytype = $listing->propertytype ?
The query worked for CITY but failed for property_type.
@FounderStartup all good. Just remember to tell us what does not work, as it is often impossible to guess :)
@Sinnbeck Thanks for understanding :)
I need a query to get "$similarlistings" with city = $listing->city and propertytype = $listing->propertytype ?
Presently the following query is not filtering the data.
controller :
$listing = Listing::findOrFail($id);
Listing::find($id)->increment('views');
$responses = ListingResponse::where('listing_id', $id)->paginate(10);
$totalresponses = ListingResponse::where('listing_id', $id)->count();
$similarlistings = Listing::with(['cityname' => function ($q) use ($listing) {
$q->where('id', $listing->city);
}])
->with(['listingpropertytypename' => function ($q) use ($listing) {
$q->where('id', $listing->property_type);
}])
->with('cityname','listingpropertytypename','listingtypename')->take(4)->get();
@Sinnbeck Oops !!! It worked with whereHas :) Can you explain why it worked and not earlier ?
@FounderStartup they do different things.
With = loads a relationship as children. If you use an array you can limit the children (not the parent). Imagine saying to a group of mothers that they can only bring girls. That still means all mothers come, even if they have no girls
WhereHas = limit the parents. Same example. We this time only want mothers who has girls.
@Sinnbeck Wow !! excellent explanation. You must start a youtube channel to teach laravel :)
@FounderStartup haha thanks I run a blog instead. Maybe I will write a post about it some day
Please sign in or create an account to participate in this conversation.