【问题标题】:Laravel scope - pass 2 parameters to scopeLaravel 范围 - 将 2 个参数传递给范围
【发布时间】:2015-04-21 03:26:41
【问题描述】:

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

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

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

控制器 sn-p 来了:

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

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

或者只有第二个作用域才有可能?

非常感谢

【问题讨论】:

    标签: php laravel scope


    【解决方案1】:

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

    您的范围

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

    你的控制器 sn-p

    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>
    

    【讨论】:

    • 在我的 laravel 7 秒参数中转换为 Illuminate\Database\Eloquent\Relations\HasMany 我如何将参数发送到范围
    猜你喜欢
    • 2017-08-17
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多