【问题标题】:Creaty an single array by a Laravel Collection, but with specific fields通过 Laravel 集合创建单个数组,但具有特定字段
【发布时间】:2021-10-02 17:39:31
【问题描述】:

我有这个模型,我正在调用它

$data = ProcessoSeletivoRDSincroniza::all();

这个模型给了我一个包含 300 多条记录的集合,包括姓名、手机、电子邮件等属性。

我必须通过数组将此集合传递给 API 主体请求,该数组具有特定的关键字段,而我考虑这样做的唯一方法是使用 foreach 循环迭代此集合,并创建/设置这个数组和这个集合字段,工作正常,但是我的应用程序对每条记录都做了一个请求,这不是处理它的好方法。

所以我在想是否有办法创建一个包含所有记录的“自定义”和单个数组,所以我不需要逐条迭代和发出请求,只需将所有这些记录转换为 JSON 文件和发送。

这是我现在的代码:

        $data = ProcessoSeletivoRDSincroniza::all();
        //$data = $data->toArray();

        $api = new RDStationApi();
        foreach($data as $row)
        {
            $events = array(
                "event_type" => "CONVERSION",
                "event_family" => "CDP",
                "payload" => [
                    "conversion_identifier" => "Name of the conversion event",
                    "name" => $row->nome_completo,
                    "email" => $row->email,
                    "personal_phone" => $row->telefone,
                    "cf_ps_curso" => $row->ps_curso,
                    "cf_ps_ano_semestre" => $row->ps_anosemestre,
                    "cf_ps_data_vestibular_agendado" => $row->ps_data_vestibular_agendado,
                    "cf_ps_nota_enem" => (string) $row->ps_nota_enem,
                    "cf_forma_ingresso" => $row->ps_forma_ingresso, 
                    "cf_ps_unidade" => $row->ps_unidade, 
                    "cf_ps_situacao" => $row->ps_situacao
                ]
                );
                $return = $api->update_contact($events);

【问题讨论】:

    标签: php arrays laravel api eloquent


    【解决方案1】:

    您可以使用名为 API-Resources 的 Laravel 功能。

    https://laravel.com/docs/8.x/eloquent-resources

    为您的模型创建一个新的Resource

    php artisan make:resource ProcessoSeletivoRDSincronizaResource
    

    之后,这将在名为的资源文件夹中创建一个文件; ProcessoSeletivoRDSincronizaResource ,在这个文件中你需要修改 toArray() 方法。

    <?php
    
    namespace App\Http\Resources;
    
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class ProcessoSeletivoRDSincronizaResource extends JsonResource
    {
        /**
         * Transform the resource into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        {
            return [
                'id' => $this->id,
                //ADD ALL THE FIELDS, methods also work normally: $this->myMethod()
                'created_at' => $this->created_at,
                'updated_at' => $this->updated_at,
            ];
        }
    }
    

    之后你可以像这样使用资源:

    //for the whole collection
     $events = ProcessoSeletivoRDSincronizaResource::collection(ProcessoSeletivoRDSincroniza::all());
    //or for single use
     $event = new ProcessoSeletivoRDSincronizaResource($single_model)
    

    【讨论】:

    • 这正是我需要的!谢谢
    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 2021-07-11
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2017-01-01
    相关资源
    最近更新 更多