我想使用带有附加列(访问器)的 Eloquent 集合, 但我不想将列附加到模型中。 任何有建议的人请帮助我。
class User extends Authenticatable
{
/**
* @return mixed
*/
public function getAdmitCardAttribute() {
if (!$this->relationLoaded('userAdmitCard')) {
$this->load('userAdmitCard');
}
return $this->userAdmitCard->first();
}
}
还有存储库代码,当我检索该列时出现“未定义列progress_video”错误
public function getAllUsers($inputs) {
$users = $this->model;
if (isset($inputs['progress']) && $inputs['progress']) {
$users = $users->whereBetween('progress_video',
[$inputs['progress']['from'], $inputs['progress']['to']]);
}
return $users;
}
最佳答案
如果您不想将其附加到模型中,可以在查询中使用 $model->append(['attributes'])
public function getAllUsers($inputs) {
$users = $this->model->append('ColumName');
if (isset($inputs['progress']) && $inputs['progress']) {
$users = $users->whereBetween('progress_video',
[$inputs['progress']['from'], $inputs['progress']['to']]);
}
return $users;
}
关于laravel - 如何使用附加列来 Eloquent 收集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56684123/