MySQL返回结果,Laravel Eloquent返回空。

huangapple go评论110阅读模式
英文:

MySQL returns results, Laravel Eloquent returns empty

问题

这让我抓狂。我有一个非常简单的查询,在MySQL中运行正确,但在Laravel Eloquent中没有返回结果:
Laravel代码没有返回结果。

$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();

当我查看查询日志时,这是运行的查询:

select * from `featured_feedback` where `company_id` = 8 and `category_id` = 1

当我直接在数据库上运行该查询时,它返回了4个结果。这让我很烦恼。这只涉及一个表,SQL严格模式为false,没有连接,查询也很干净。有什么想法吗?

英文:

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

答案1

得分: 0

您正在将多个条件链接到`$feedback`查询构建器实例上,但未将最终结果捕获回`$feedback`变量中。

$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();
英文:

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();

答案2

得分: 0

在“FeaturedFeedback”模型中添加一个关联:

public function category(){
    return $this->belongsTo(Category::class);
}

使用when条件代替if条件:

$feedback = FeaturedFeedback::where("company_id", $company->id)->when(!empty($request->category), function ($query) use ($request) {
    $query->whereHas('category', function ($query) use ($request) {
        $query->where("name", $request->category);
    });
})->get();
英文:

In the "FeaturedFeedback" model add a relationship

public function category(){

   return $this->belongsTo(Category::class);
}

use when conditioning instead of if condition

$feedback = FeaturedFeedback::where("company_id",$company->id)->when(!empty($request->category),function ($query)use($request){

            $query->whereHas('category',function ($query)use($request){
                $query->where("name",$request->category);
            });
        })->get();

huangapple
  • 本文由 发表于 2023年8月11日 00:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定