Suppose I have a Homework Eloquent model like this :
class Homework extends Model
{
protected $primaryKey = 'homework_id';
protected $dates = ['deadline'];
protected $appends = ['deadline_picker'];
protected $fillable = ['lesson_id', 'teacher', 'title', 'desc', 'deadline', 'address', 'size', 'type'];
public function getDeadlineAttribute ($value)
{
return date('d/m/Y H:i', strtotime($value));
}
public function getDeadlinePickerAttribute ()
{
return date('l d F Y-H:i', strtotime($this->attributes['deadline']));
}
}
As you see I define a new Accessor (deadline_picker) for deadline field.
Now I want to select only title of this model like this :
public function Homeworks ()
{
$homeworks =
Homework::where('deadline', '>=', Carbon::now())->get(['title']);
return $homeworks;
//return view('pages/homeworks', compact('homeworks'));
}
But after running above code, bellow error is shown:
ErrorException in Homework.php line 30:
Undefined index: deadline
Seems that in this case, getting deadline field in ->get(['title']); is required (means ->get(['title','deadline']);).
Why it happens?