【问题标题】:Laravel change every model in collectionLaravel 更改集合中的每个模型
【发布时间】: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

标签: php laravel eloquent


【解决方案1】:

您可以通过多种方式实现这一目标。

每个Eloquent collection 都扩展了Collection 类,让您可以使用map() 或each() 等有用的方法:

// get your collection
$trainees = Trainee::all();

// customize them
$trainees->each(function ($trainee) {
  $trainee->id = encrypt($item->id);
});

return $trainees;

PS:默认情况下,当将数组/集合返回到 API 时,Laravel 会将其返回为 JSON。


第二种方法,我认为更细化和推荐,是使用API Resources。来自文档:

在构建 API 时,您可能需要一个转换层,该层位于 在您的 Eloquent 模型和实际的 JSON 响应之间 返回给您的应用程序的用户。 Laravel 的资源类允许 您可以富有表现力且轻松地转换您的模型和模型 集合成 JSON。

因此,您需要:

1。生成您的 API 资源

php artisan make:resource TraineeResource

2。自定义它

App\Http\Resources\TraineeResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class TraineeResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => encrypt($this->id),
            // ...
        ];
    }
}

3。应用它

在您的控制器中:

App/Http/Controllers/MyCoolController.php

use App\Http\Resources\TraineeResource;

public function aCoolMethod(Request $request)
{
    // get your collection
    $trainees = Trainee::all();

    // return it
    return TraineeResource::collection($trainees);
}

【讨论】:

    【解决方案2】:

    您可以使用map 函数更改collection 中每个项目的数据。

    return $trainees
        ->map(function ($item) {
            $item->id = decrypt($item->id);
            return $item;
        })
        ->toJson();
    

    【讨论】:

    • 这会产生一个Call to undefined method Illuminate\Database\Eloquent\Builder::map()
    • 查询似乎还没有执行。在-&gt;map 函数之前添加-&gt;get()。
    • 您需要分配get() 函数的输出。 $trainees = $trainees-&gt;get();
    • 但是 ID 没有被加密。它仍然是一个整数
    • @KuebelElch15 因为您正在解密而不是加密。尝试这样做:encrypt($item-&gt;id);
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2015-08-08
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多