Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel scope - pass 2 parameters to scope

Tags:

scope

php

laravel

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

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 and forwarded. The link passes two parameters, how can i pass them to the scope?

Or is it only possible with a second scope?

many thanks

like image 770
Jack Avatar asked Feb 19 '15 16:02

Jack


1 Answers

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>
like image 115
Marwelln Avatar answered Sep 20 '22 09:09

Marwelln