如何在子请求上设置活动条件?
How set active condition on subrequest?
由于 Product 模型与 Category 具有多对多关系(使用 ProductCategory 模型)。
我尝试在请求中添加附加条件。
$productsWithActiveCategories = Product
::getByTitle($filter_title)
->getByStatus('A')
->getByPublishedAt($filter_date_from, '>=')
->getByPublishedAt($filter_date_till, '<')
->with('creator')
->with('productCities')
->with('productCategories') // product_categories
->with('productCategories.category')
->getByActiveCategory( true)
->orderBy('regular_price', 'asc')
->get();
但是我得到了错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ts_categories.active' in 'where clause'
select
*
from
`ts_products`
where
`ts_products`.`title` like % no %
and `ts_products`.`status` = A
and ts_products.published_at >= 2022 -04 -01
and ts_products.published_at < 2022 -04 -21
and `ts_categories`.`active` = 1
order by
`regular_price` asc
在app/Models/Product.php模型中我添加了:
public function scopeGetByActiveCategory($query, $active = null)
{
if ( ! isset($active) or strlen($active) == 0) {
return $query;
}
return $query->where(with(new Category)->getTable() . '.active', $active);
}
似乎出现了错误,因为此范围应用于第一个请求,而不是包含所有 productCategories 和
productCategories.category如我所愿。
如何将此范围设置为 productCategories.category 关系?
更新块:
在 app/Models/Product.php 我添加了 2 个方法:
public function categories()
{
return $this->hasManyThrough(ProductCategory::class, Category::class);
}
public function scopeOnlyActiveCategories()
{
return $this->hasManyThrough(ProductCategory::class, Category::class)
->where(with(new Category)->getTable() . '.active', true);
}
但是当我尝试在请求中使用第二种方法时:
$productsWithActiveCategories = Product
::getByTitle($filter_title)
->getByStatus('A')
->getByPublishedAt($filter_date_from, '>=')
->getByPublishedAt($filter_date_till, '<')
->onlyActiveCategories()
->with('creator')
->with('productCities')
->with('productCategories') // product_categories
->with('productCategories.category')
->orderBy('regular_price', 'asc')
->get();
我收到错误:
Column not found: 1054 Unknown column 'ts_categories.product_id' in 'field list'
怎么了?
谢谢!
您可以在 whereHas 中使用点嵌套符号来过滤您的模型,而不是 ->getByActiveCategory(true)
,如下所示:
$productsWithActiveCategories = Product
(...)
->whereHas('productCategories.category', function ($category) {
$category->where('active', 1);
})
(...)
->get();
由于 Product 模型与 Category 具有多对多关系(使用 ProductCategory 模型)。
我尝试在请求中添加附加条件。
$productsWithActiveCategories = Product
::getByTitle($filter_title)
->getByStatus('A')
->getByPublishedAt($filter_date_from, '>=')
->getByPublishedAt($filter_date_till, '<')
->with('creator')
->with('productCities')
->with('productCategories') // product_categories
->with('productCategories.category')
->getByActiveCategory( true)
->orderBy('regular_price', 'asc')
->get();
但是我得到了错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ts_categories.active' in 'where clause'
select
*
from
`ts_products`
where
`ts_products`.`title` like % no %
and `ts_products`.`status` = A
and ts_products.published_at >= 2022 -04 -01
and ts_products.published_at < 2022 -04 -21
and `ts_categories`.`active` = 1
order by
`regular_price` asc
在app/Models/Product.php模型中我添加了:
public function scopeGetByActiveCategory($query, $active = null)
{
if ( ! isset($active) or strlen($active) == 0) {
return $query;
}
return $query->where(with(new Category)->getTable() . '.active', $active);
}
似乎出现了错误,因为此范围应用于第一个请求,而不是包含所有 productCategories 和 productCategories.category如我所愿。
如何将此范围设置为 productCategories.category 关系?
更新块: 在 app/Models/Product.php 我添加了 2 个方法:
public function categories()
{
return $this->hasManyThrough(ProductCategory::class, Category::class);
}
public function scopeOnlyActiveCategories()
{
return $this->hasManyThrough(ProductCategory::class, Category::class)
->where(with(new Category)->getTable() . '.active', true);
}
但是当我尝试在请求中使用第二种方法时:
$productsWithActiveCategories = Product
::getByTitle($filter_title)
->getByStatus('A')
->getByPublishedAt($filter_date_from, '>=')
->getByPublishedAt($filter_date_till, '<')
->onlyActiveCategories()
->with('creator')
->with('productCities')
->with('productCategories') // product_categories
->with('productCategories.category')
->orderBy('regular_price', 'asc')
->get();
我收到错误:
Column not found: 1054 Unknown column 'ts_categories.product_id' in 'field list'
怎么了?
谢谢!
您可以在 whereHas 中使用点嵌套符号来过滤您的模型,而不是 ->getByActiveCategory(true)
,如下所示:
$productsWithActiveCategories = Product
(...)
->whereHas('productCategories.category', function ($category) {
$category->where('active', 1);
})
(...)
->get();