我这里有两个问题需要解决:
- 如何减少代码中的冗余?
$this->all()->lists('name', 'id')->all()
- 如何通过
orderBy
方法调用改变器(mutator)?
helper.php
if (! function_exists('withEmpty')) {
function withEmpty($selectList, $emptyLabel = '')
{
return array('' => $emptyLabel) + $selectList;
}
}
Agent.php(模型)
public function getNameAttribute($value){
return $this->lname.', '.$this->fname.' '.$this->mname;
}
public function listAgents($emptyLabel = '--Select Agent--'){
return withEmpty($this->all()->lists('name', 'id')->all(), $emptyLabel);
}
lists()
函数。 - Alexey Mezeninall()->lists('name', 'id')
。对于列,您不需要先执行查询。因此,当您使用all()->listst()
时,您正在使用集合方法而不是Eloquent构建器方法。 - Alexey Mezenin