manshu
137
17
Eloquent

Trying to get values from the collection

Posted 1 week ago by manshu

I am trying to create a query, but having issue. I have Listing that has 2 columns city_id and state_id. I am trying to get result where city_it = city.id model and similar for state.

my route = https://example.com/{state}/{city}/

So if user tries to access some city, they will see all listings for that state. Please help. Thanks

        $state = State::where('name', $state)->get();
        $city = City::where('name', $city)->get();
        $listings = Listing::where(function ($query) use ($state, $city) {
            $query->where('city_id', $city)->get();
            $query->where('state_id', $state)->get();
        });
        return $listings;

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.