【问题标题】:How to add elements to array in for loop, PHP - Laravel如何在 for 循环中向数组添加元素,PHP - Laravel
【发布时间】:2021-01-18 02:11:41
【问题描述】:

我正在连接到存储在数组 $firmy 中的 3 个数据库 目前它们被硬编码到数组中,但它们将通过 Axios 请求设置。

public function show(Request $request, $division, $id)
{
    $firmy = array('connection1', 'connection2', 'connection3');
    $data = [];

    foreach ($firmy as $firma) {
        DB::setDefaultConnection($firma);

        $calendar = new CalendarEvent();
        $data[] = CalendarEventResource::collection($calendar->with('calendarCategories')->where('start', '>', '2020-05-21')->get());

        DB::purge($firma);
    }
    foreach ($data as $firma_event) {
        foreach ($firma_event as $event) {
            $eventT[] = $event;
        }
    }
    return $eventT;
}

我设置连接,收集并关闭连接。 在这种情况下是 3 次。

然后我遍历数据以一次性获取所有记录。 这是$eventT数组返回的API响应:

[{"id":17549,"title":"Test","description":"test","contact":"test","email":"test","cat":1,"approved":0,"kto_dodal":450,"calendarCategories":{"id":1,"name":"Ogolna","color":"blue"},"start":"2020-09-30","end":"2020-09-30","private":0,"created_at":null,"updated_at":null},

{"id":17580,"title":"Test","description":"test","contact":"test","email":"test","cat":1,"approved":0,"kto_dodal":450,"calendarCategories":{"id":1,"name":"Ogolna","color":"blue"},"start":"2020-09-30","end":"2020-09-30","private":0,"created_at":null,"updated_at":null},

{"id":17545,"title":"Test","description":"test","contact":"test","email":"test","cat":1,"approved":0,"kto_dodal":450,"calendarCategories":{"id":1,"name":"Ogolna","color":"blue"},"start":"2020-09-30","end":"2020-09-30","private":0,"created_at":null,"updated_at":null}]

每个连接/表一个,这很好。

我想为每条记录添加连接名称。所以 API 响应看起来像这样:

{"id":17545,"title":"Test","description":"test","contact":"test","email":"test","cat":1,"approved":0,"kto_dodal":450,"calendarCategories":{"id":1,"name":"Ogolna","color":"blue"},"start":"2020-09-30","end":"2020-09-30","private":0,"created_at":null,"updated_at":null, "firma":connection1}]

所以"firma":nameOfConnection 添加到每条记录中。

我尝试循环遍历data[] 并使用array_push,但我无法将连接值放在每条记录中。 值在对象之外结束:

0: {id: 17549, title: "Test", description: "Test ",…} 1: {firma: "connection1"} firma: "connection1"

【问题讨论】:

  • 我认为您不会使用array_push,因为您只是在数组中添加一个新元素。相反,您希望将新的对象属性添加到 现有 数组元素。也许将行更改为:``` $result = CalendarEventResource::collection(...etc...)->get()); $result->firma = $firma"; $data[] = $result;```
  • 感谢您的回复。我确实尝试过这种方法,但它没有将firma 添加到集合中。
  • 包含您尝试过的代码可能会对您有所帮助。
  • 对不起,它是:$result = CalendarEventResource::collection($calendar->with('calendarCategories')->where('start', '>', '2020-05-21')->get()); $result->firma = $firma; $data[] = $result; dd($data); 这是 dd 的结果:array:1 [▼ 0 => Illuminate\Http\Resources\Json\AnonymousResourceCollection {#1287 ▼ +collects: "App\Http\Resources\CalendarEventResource" +collection: Illuminate\Support\Collection {#1291 ▶} +resource: Illuminate\Support\Collection {#1291 ▶} +additional: [] +"firma": "connection1" } ]
  • 将其添加到您的问题中,以便正确格式化。

标签: php laravel vue.js axios


【解决方案1】:

我设法对其进行了排序。 首先,我在 Resource 类中添加了一个值 firma,因此它被添加到集合中,尽管它将是 null,因为 DB 中没有具有该名称的列:

    public function toArray($request)
    {
        return [
            'id'                    => $this->id,
            'title'                 => $this->title,
            'description'           => $this->description,
            'contact'               => $this->contact,
            'email'                 => $this->email,
            'cat'                   => $this->cat,
            'approved'              => $this->approved,
            'kto_dodal'             => $this->kto_dodal,
            'calendarCategories'    => new CalendarCategoryResource($this->whenLoaded('calendarCategories')),
            'start'                 => $this->start,
            'end'                   => $this->end,
            'private'               => $this->private,
            'created_at'            => $this->created_at,
            'updated_at'            => $this->updated_at,
            'firma'                 => $this->firma,
        ];
    }

然后我访问事件集合,循环遍历结果并将firma 的值设置为循环当前的值。

public function show(Request $request, $division, $id)
    {
        $firmy = array('connection1', 'connection2', 'connection3');
        $data = [];

        foreach ($firmy as $firma) {
            DB::setDefaultConnection($firma);

            $calendar = new CalendarEvent();
            $data = CalendarEventResource::collection($calendar->with('calendarCategories')
                ->where('start', '>', '2020-09-21')
                ->where('private', '=', '0')
                ->get());

            // Loop over data in collection and set value of firma
            foreach ($data as $value) {
                $value->firma = $firma;
                $total[] = $value;
            }

            DB::purge($firma);
        }

        return $total;
    }

这是对象内部带有firma属性的返回值:

{"id":17545,"title":"Test","description":"test","contact":"test","email":"test","cat":1,"approved":0,"kto_dodal":450,"calendarCategories":{"id":1,"name":"Ogolna","color":"blue"},"start":"2020-09-30","end":"2020-09-30","private":0,"created_at":null,"updated_at":null,"firma":"connection1"},

【讨论】:

    猜你喜欢
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多