Laravel scope-将2个参数传递到scope


Laravel scope - pass 2 parameters to scope

我的表中有一个状态列,包含3种状态(register、forwarded、done)。

我在我的视图中有一个不同的链接来过滤结果:

<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>

以下是控制器片段:

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

这里的范围:

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

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

还是只有第二个范围才有可能?

非常感谢

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

您的范围

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);
}

您的控制器片段

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);
}

您的链接

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