在运行时在集合上添加属性追加

时间:2019-01-17 04:34:34

标签: laravel laravel-5 attributes append

 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
  }

1 个答案:

答案 0 :(得分:0)

您尝试使用pluck吗?

Student::whereIn('id',[1,4,10,21])->pluck('group');

基于 Retrieving A List Of Column Values