LoginSignup
4
4

More than 5 years have passed since last update.

Laravel4 動的スコープ

Posted at

ex)http://laravel4.kore1server.com/docs/eloquent#query-scopes

Laravel4動的スコープ

クエリーを構築するときにパラメータがあったら条件をつける形で動的にクエリーを構築するようにしたかった。

model.php

public function scopeAddWhere( $query, $input )
{
    foreach( $input as $key => $value ){
        if( empty( $value ) ) continue;
        switch( $key ){
            case 'pref':
                $query->where( $key, '=', $value);
            break;
    }
    return $query;
}

controller.php

Model::addWhere( $input )->get()

呼び出し時には、「scope」を外す。

OK

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4