gpt4 book ai didi

php - 拉维尔 5 : Eloquent whereHas subquery doesn't filter results

转载 作者:行者123 更新时间:2023-11-29 03:22:03 25 4
gpt4 key购买 nike

我正在尝试使用关系从模型中检索一些结果,并且正在尝试对该关系应用一些过滤器。

这是模型:

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserProduct extends Model
{

protected $primaryKey = null;
public $incrementing = false;
protected $table = "user_product";

public $fillable = [
...
"product_id",
"user_id",
"is_featured",
"is_hidden_from_latest"
...
];

public function product()
{
return $this->belongsTo("\\App\\Models\\Product", "product_id", "id");
}

...

}

这是相关模型:

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
protected $table = "products";

public $timestamps = false;

public $fillable = [
...
];

public function userProduct()
{
return $this->hasOne("\\App\\Models\\UserProduct", "product_id", "id");
}

...

}

下面是对UserProduct模型和产品关系的查询:

$user_products = UserProduct::with("product")
->whereHas("product", function($q) {
$q->where("product_status", "live")
->where("parent_child", "Child");
})->where("is_featured", 1)
->orWhere("is_hidden_from_latest", 0)
->orderBy("is_featured", "desc")
->orderBy("updated_at")
->get();

问题是 whereHas 子查询似乎没有过滤任何东西,无论我为每个 product_statusparent_child 使用什么值进行比较>.

我有没有做错什么?


更新:似乎游戏破坏者是最后的这两个 where() 语句:

....
->where("is_featured", 1)
->orWhere("is_hidden_from_latest", 0)
....

更具体地说是 orWhere() 语句。

最佳答案

试试这个

$user_products = UserProduct::with("product")
->whereHas("product", function($q) {
$q->where("product_status", "live")
->where("parent_child", "Child");
})
->where(function ($query) {
$query->where("is_featured", 1)
->orWhere("is_hidden_from_latest", 0);
})
->orderBy("is_featured")
->orderBy("updated_at")
->get();

关于php - 拉维尔 5 : Eloquent whereHas subquery doesn't filter results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42719664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com