2016-08-22 69 views
1

我有一个方法:从配置读入方法?

public function getAllRecords($perPage = 10){ 
    .... 
} 

如果每个页面没有指定,它会得到10

我想读从配置这个数字。

我已经试过:

public function getAllRecords($perPage = config('db.perPage')){ 

但我得到一个错误。

如何将配置读入这种方法?

回答

1

您可以创建一个构造函数和得到这个东西有:

protected $perPage; 

public function __construct() 
{ 
    $this->perPage = config(db.perPage); 
} 

public function getAllRecords($perPage = $this->perPage) 
{ 

或者你可以这样做:

public function getAllRecords($perPage = null) 
{ 
    $perPage = is_null($perPage) ? config('db.perPage') : $perPage; 
1

我倾向于做这样的事情:

public function getAllRecords($perPage = null) 
{ 
    if (is_null($perPage)) { 
     $perPage = config('db.perPage'); 
    } 

    // ... 
}