Laravel Nova指标过滤 [英] Laravel Nova metrics filtering

查看:100
本文介绍了Laravel Nova指标过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Property的模型,该模型具有"active"标志.我希望在资源顶部显示一个指标,以显示活动属性的数量.

I have a model called Property which has an 'active' flag. I want a metric at the top of my resource which shows a count of active Properties.

我的calculate方法与文档中的完全相同,但这显示了所有属性,而不是活动属性:

My calculate method is exactly as in the doc but this shows all Properties rather than active ones:

public function calculate(Request $request)
{
    return $this->count($request, Property::class);
}

如何添加过滤器?

我尝试了where子句:

I've tried a where clause:

public function calculate(Request $request)
{
    return $this->count($request, Property::class)->where('active','=',1);
}

和查询范围:

public function calculate(Request $request)
{
    return $this->count($request, Property::class)->active();
}

我认为我也许可以使用在资源列表页面上设置的Nova过滤器,但这似乎也不起作用.我敢肯定这确实很容易,但是我还没有解决.感谢您的帮助!

I thought I might be able to use the Nova filter I set up on the resource list page but that didn't seem to work either. I'm sure it's really easy but I haven't worked it out. Thanks for your help!

推荐答案

您可以在$ model参数中使用每种类型的Eloquent \ Builder实例.

Your can use every type of Eloquent\Builder instance in the $model param.

代替:

public function calculate(Request $request)
{
    return $this->count($request, Property::class);
}

在模型上设置范围

App\Property.php
...
public function scopeActive($query)
{
    return $query->where('active', 1);
}
public function scopeInactive($query)
{
    return $query->where('active', 0);
}

并将此范围用作您的calculate方法中的$ model参数,因为该范围的调用会返回一个Eloquent \ Builder实例

And use this scope as the $model param in your calculate method, because the call of the scope returns a Eloquent\Builder Instance

public function calculate(Request $request)
{
    return $this->count($request, Property::active());
    // return $this->count($request, Property::inactive());
}

修改当然,您可以使Eloquent Builder调用内联:

Edit Of course you can make the Eloquent Builder call inline:

public function calculate(Request $request)
{
    return $this->count($request, Property::where('active', 1));
}

这篇关于Laravel Nova指标过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆