51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

英文:

MySQL returns results, Laravel Eloquent returns empty

问题 {#heading}

这让我抓狂。我有一个非常简单的查询,在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 {#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 {#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();


赞(1)
未经允许不得转载:工具盒子 » MySQL返回结果,Laravel Eloquent返回空。