This is driving me nuts. I have a super simple query that runs correctly in MySQL but returns no results in Laravel Eloquent:
Laravel Code that returns no results.
$feedback = FeaturedFeedback::where("company_id", "=", $company->id);
if ($request->category != "") {
$category = Category::where("name", "=", $request->category)->first();
}
if ($category) {
$feedback->where("category_id", "=", $category->id);
}
$feedback->get();
When I look at the query logs, this is the query that runs:
select * from `featured_feedback` where `company_id` = 8 and `category_id` = 1
When I run that query directly on the DB, it returns 4 results. Driving me nuts. It’s one table, SQL strict mode is false, no joins, clean query. Thoughts??
>Solution :
You’re chaining multiple conditions onto the $feedback query builder instance but not capturing the final result back into the $feedback variable.
$feedbackQuery = FeaturedFeedback::where("company_id", "=", $company->id);
if ($request->category != "") {
$category = Category::where("name", "=", $request->category)->first();
}
if ($category) {
$feedbackQuery->where("category_id", "=", $category->id);
}
$feedback = $feedbackQuery->get();