Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL returns results, Laravel Eloquent returns empty

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??

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading