class Student extends Model
{
.... some code here
public function getGroupAttribute()
{
if($this->group == 1)
return 'Group A';
else
return 'No Group';
}
}
我知道有一个函数setAppends(),但是那仅用于单个模型。 而且我也知道在类中将$ appends变量定义为的静态方式:
protected $appends = ['group']; // but this is not changable if I did it like this.
如何在运行时在控制器上实现set $ append?
//inside controller
public function getStudent()
{
Student::whereIn('id',[1,4,10,21])->get(); // I want to set the $appends value here
}
答案 0 :(得分:0)
您尝试使用pluck
吗?
Student::whereIn('id',[1,4,10,21])->pluck('group');