【发布时间】:2018-11-14 16:06:09
【问题描述】:
我有一个项目集合。现在,在通过我的 api 发送它之前,我想更改模型的值(但我不想更新数据库中的模型)。
现在我想遍历我的集合并将其作为 json 返回,但我收到了invalid Payload。
这是我执行的代码:
$trainees = Trainee::select();
if(!$request->user()->hasPermission('read-trainees')) {
$trainees->where('status', 1)->where('visible', 1);
} else {
$trainees->with(array('user'=>function($query){
$query->select('id','firstname', 'lastname');
}));
$trainees->select('user_id');
}
$trainees->select('interested_jobs', 'graduation');
$trainees = $trainees->get();
return $trainees
->map(function ($item) {
$item->id = encrypt($item->id);
return $item;
})
->toJson();
【问题讨论】:
-
而问题只出在json转换上?值变了,
$trainees是你想要的数组? -
解密项目ID?它是从数据库中加密而来的?
-
我不知道收藏是否正在更新。没有代码一切正常,但有了这段代码就不行了
-
解密对我来说是愚蠢的。但现在我得到
call do undefined function toJson() -
@KuebelElch15 当您执行
Trainee::each(...)时不会保存集合,只是遍历每个项目。检查this