Как получить первый и последний uid объекта в foreach l oop? - PullRequest
0 голосов
/ 31 марта 2020

Я хочу получить первую и последнюю итерацию uid и сохранить в базе данных. Я использую пакет webklex для извлечения писем из gmail и сохранения в локальной базе данных, в зависимости от требований, мне нужен идентификатор объекта первой и последней итерации, но не могу ничего получить вместо ошибки. Как справиться с этой ситуацией?

Ошибка:

{ "message": "Indirect modification of overloaded property Webklex\\IMAP\\Message::$uid has no effect", "exception": "ErrorException", "file": "/var/www/site/app/Http/Controllers/FetchMailController.php", "line": 111, "trace": [ { "file": "/var/www/site/app/Http/Controllers/FetchMailController.php", "line": 111, "function": "handleError", "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions", "type": "->" }, { "function": "index", "class": "App\\Http\\Controllers\\FetchMailController", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Controller.php", "line": 54, "function": "call_user_func_array" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php", "line": 45, "function": "callAction", "class": "Illuminate\\Routing\\Controller", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 219, "function": "dispatch", "class": "Illuminate\\Routing\\ControllerDispatcher", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 176, "function": "runController", "class": "Illuminate\\Routing\\Route", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 680, "function": "run", "class": "Illuminate\\Routing\\Route", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 30, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php", "line": 41, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php", "line": 75, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php", "line": 49, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php", "line": 56, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Session\\Middleware\\StartSession", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php", "line": 37, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php", "line": 66, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Cookie\\Middleware\\EncryptCookies", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 104, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 682, "function": "then", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 657, "function": "runRouteWithinStack", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 623, "function": "runRoute", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 612, "function": "dispatchToRoute", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 176, "function": "dispatch", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 30, "function": "Illuminate\\Foundation\\Http\\{closure}", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php", "line": 21, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php", "line": 21, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php", "line": 27, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php", "line": 62, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/fideloper/proxy/src/TrustProxies.php", "line": 57, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Fideloper\\Proxy\\TrustProxies", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 104, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 151, "function": "then", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/var/www/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 116, "function": "sendRequestThroughRouter", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" }, { "file": "/var/www/site/public/index.php", "line": 55, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" } ] }

Код:

$user_mails                   =  array();
                $emails_for_db                =  array();
                $emails_for_db_user_message   =  array();

                foreach ($aFolder as $oFolder) {
                    $aMessage = $oFolder->messages()->limit(100, 1)->get();




                    $user_mails[] = $aMessage;

                    foreach ($aMessage as $oMessage){


                        $last_array_value = end($oMessage->uid);

                        dd($last_array_value);

                        $emails_for_db[] = [

                            'gmail_message_id'    => $oMessage->uid,
                            'subject'             => $oMessage->getSubject(),
                            'body'                => $oMessage->getHTMLBody(true),
                            'sender_email'        => $oMessage->getFrom()[0]->mail,
                            'sender_name'         => $oMessage->getSender()[0]->personal,
                            'date'                => $oMessage->ON,
                        ];

                        $emails_for_db_user_message[] = [

                            'user_id'             => $this->login_user_id,
                            'placeholder'         => 'inbox',
                            'is_read'             => $oMessage->getFlags()['seen'],
                            'is_starred'          => $oMessage->getFlags()['flagged'],
                        ];
                    }
                }

1 Ответ

2 голосов
/ 31 марта 2020

На самом деле вам не нужно итерировать массив, чтобы получить первый и последний элементы, вы можете просто использовать: $array[0], чтобы получить первый элемент из списка и $array[count($array) - 1] для последнего элемента. .

В вашем примере:

Первый: $user_mails[0]->id Последний: $user_mails[count($user_mails) - 1]->id

Или вы можете сделать это с помощью Eloquent:

$first = ModelName::orderBy('id')->first()->id;

$last = ModelName::orderBy('id', 'DESC')->first()->id;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...