Do you get results from the following?
Country::all()
If so, then it would seem that you're filtering in such a way that no matches are found. If not, then perhaps the Country model can't be found.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to get "Top 10" results for travel destinations: Countries, Cities, Things to Do. I am using the query builder and OrderByRaw to order destinations by "Upvotes - Downvotes" so the top 10 user voted destinations in each category will be fetched from the DB.
This is my basic query :
Destination::where('type', '=', 'destination_type')->orderByRaw('(upvotes - downvotes) desc')->take(10)->get();
I have Classes for each type of Destination: Country, City, PointOfInterest (for things to do). So my Country specific query actually looks like:
Country::where('type', '=', 'country')->orderByRaw('(upvotes - downvotes) desc')->take(10)->get();
Basicaly the same query is used for each class and I just swap out the Class name and 'type' to reflect such.
All destinations live in the same DB table. All have the same columns, all have Upvotes and Downvotes, although many values in these 2 columns are still '0', but not specifically 'NULL'.
Both Cities and Points Of Interest Top 10 results are being fetched and displaying as expected. But the Country results are coming back as NULL. Literally nothing is being fetched. I definitely have countries and some countries have vote values in the respective columns.
If I remove the OrderByRaw clause from the Country query, i can fetch ALL countries. But with the clause I get a NULL set.
I have confirmed no typos in the clause, I have swapped the 'type' value to be 'city' instead of country, both the (exact same) city and points of interest queries work. I have tried a raw SQL query in my DB and can retrieve the expected results. But I can't fetch the results from within my app for Countries.
Any advice?
Since I could not get the query to work with the calculation of 'upvotes - downvotes', in the end I just scrapped the downvotes field and am using just 'upvotes' as my OrderBy:
Country::where('type', '=', 'country')->orderByRaw('upvotes desc')->take(10)->get();
It gets the job done, but is not 100% accurate to what I was hoping to display and how I was hoping to display it. Oh well.
Please or to participate in this conversation.