Laravel 范围 - 将 2 个参数传递给范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28612234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:58:40  来源:igfitidea点击:

Laravel scope - pass 2 parameters to scope

phplaravelscope

提问by Hyman

I have a status column in my table with 3 states (register, forwarded, done).

我的表中有一个状态列,有 3 个状态(注册、转发、完成)。

I have a different links in my view for filter the results:

我的视图中有不同的链接用于过滤结果:

<li>{{ link_to_route('psa.index', 'Eingetragen', array('f' => 'register'), $attributes = array('title' => 'Eingetragen!' )) }}</li>
<li>{{ link_to_route('psa.index', 'Wird erledigt', array('f' => 'forwarded'), $attributes = array('title' => 'Wird erledigt!' )) }}</li>
<li>{{ link_to_route('psa.index', 'Unerledigt', array('f' => 'forwarded', 'register'), $attributes = array('title' => 'Wird erledigt!' )) }}</li>

Here comes the controller snippet:

这是控制器代码段:

if(Input::get('f'))
        {
            $reports = PsaReports::filter(Input::get('f'))->with('automat', 'failure')->orderBy('incoming_day', 'desc')->orderBy('incoming_time', 'desc')->paginate(30);
        }

And here the scope:

这里的范围:

public function scopeFilter($filter, $search)
    {
        return $filter->where('status', $search);
    }

With the third link above i want to scope the status register andforwarded. The link passes two parameters, how can i pass them to the scope?

通过上面的第三个链接,我想确定状态寄存器的范围转发。链接传递两个参数,我如何将它们传递给作用域?

Or is it only possible with a second scope?

还是只能使用第二个范围?

many thanks

非常感谢

回答by Marwelln

You can add a third parameter in your scope and then do something special if that parameter is set.

您可以在作用域中添加第三个参数,然后在设置了该参数时执行一些特殊操作。

Your scope

您的范围

public function scopeFilter($filter, $search, $search2 = null)
{
    if ($search2 !== null) {
        return $filter->where(function($query) use ($search, search2) {
            $query->where('status', $search)->orWhere('status', $search2);
        });
    }

    return $filter->where('status', $search);
}

Your controller snippet

你的控制器片段

if(Input::get('f'))
{
    $reports = PsaReports::filter(Input::get('f'), Input::get('f2'))->with('automat', 'failure')->orderBy('incoming_day', 'desc')->orderBy('incoming_time', 'desc')->paginate(30);
}

Your link

您的链接

<li>{{ link_to_route('psa.index', 'Unerledigt', array('f' => 'forwarded', 'f2' => 'register'), $attributes = array('title' => 'Wird erledigt!' )) }}</li>