Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在for循环中向数组添加元素,PHP-Laravel_Php_Laravel_Vue.js_Axios - Fatal编程技术网

如何在for循环中向数组添加元素,PHP-Laravel

如何在for循环中向数组添加元素,PHP-Laravel,php,laravel,vue.js,axios,Php,Laravel,Vue.js,Axios,我正在连接存储在数组中的3个数据库$firmy 目前,它们已硬编码到阵列中,但将通过Axios请求进行设置 public function show(Request $request, $division, $id) { $firmy = array('connection1', 'connection2', 'connection3'); $data = []; foreach ($firmy as $firma) { DB::setDefaultCo

我正在连接存储在数组中的3个数据库
$firmy
目前,它们已硬编码到阵列中,但将通过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次

然后我循环遍历数据,一次性获得所有记录。 下面是
$event
数组返回的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”,“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”,“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”:连接的名称添加到每个记录中

我尝试在
数据[]
中循环并使用
数组推送
,但我无法将连接值放入每个记录中。 值在对象外部结束:

0:{id:17549,title:“Test”,description:“Test”,…}1:{firma:“connection1”}firma:“connection1”
我设法对它进行了排序。 首先,我向资源类添加了一个
firma
值,这样它就被添加到集合中,尽管它将是
null
,因为数据库中没有具有该名称的列:

    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"},

我认为您不会使用
array\u push
,因为您只需向数组中添加一个新元素即可。相反,您希望向现有数组元素添加新的对象属性。可能会将行更改为:``$result=CalendarEventResource::collection(…等…)->get()$result->firma=$firma”;$data[]=$result;``谢谢您的回复。我确实尝试过这种方法,但它没有将
firma
添加到集合中。包含您尝试过的代码可能会对您有所帮助。很抱歉,这是:
$result=CalendarEventResource::collection($calendar->with('calendarCategories')->其中('start'、'>'、'2020-05-21')->get();$result->firma=$firma;$data[]=$result;dd($data);
下面是dd:
数组的结果:1[▼   0=>Illumb\Http\Resources\Json\AnonymousResourceCollection{#1287▼     +收集:“App\Http\Resources\CalendarEventResource”+收集:illumb\Support\collection{#1291▶}     +资源:illumb\Support\Collection{#1291▶}     +附加:[]+“firma”:“connection1”}]
将其添加到您的问题中,以便正确格式化。