我正在使用laravel 5.5,我在Product.php
模型中为我的产品制作了范围:
public function scopeApproved($query)
{
return $query->where('publish', 1);
}
取自laravel网站。
然后我会在下面显示以下代码来显示我的搜索结果。
public function search()
{
$search = request('search');
$searchType = request('searchType');
if (strcmp($searchType, "posts") == 0) {
$posts = Post::where('title', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->get();
} elseif (strcmp($searchType, "products") == 0) {
$products = Product::where('title', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->get();
}
return view('frontend.search', compact('posts', 'products'));
}
即使我的产品publish
列设置为0
仍会在搜索结果中弹出,问题仍然存在。
同样适用于产品列表,仍然在网站上显示。
我该如何解决?
答案 0 :(得分:0)
尝试:
template<typename E2> VecSum<int, E2>{ /*stuff goes here*/ }
template<typename E1> VecSum<E1, int>{ /*stuff goes here*/ }
template<typename E2> VecSum<float, E2>{ /*stuff goes here*/ }
template<typename E1> VecSum<E1, float>{ /*stuff goes here*/ }
template<typename E2> VecSum<double, E2>{ /*stuff goes here*/ }
template<typename E1> VecSum<E1, double>{ /*stuff goes here*/ }
答案 1 :(得分:0)
您的问题是您正在添加本地范围而非全局范围。
您仍然可以使用Product::approved()
但是对于全局范围,您需要在Product.php
中创建启动方法,如下所示:
protected static function boot()
{
parent::boot();
static::addGlobalScope('publish', function (Builder $builder) {
$builder->where('publish', 1);
});
}
如果您想要没有全局范围的产品Product::withoutGlobalScopes()->get();
应该这样做。