Eloquent each() method returns zero value





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















This question is related to Eloquent save() method not working inside for loop.



PROCESS MAP: UPLOAD FILES > CREATE TABLE ROWS WITH DEFAULT VALUE 0 > CONTINUE TO ITERATE OVER 0 PARTS(Eg If last one 3-> 4,5,6 )



I want to continue add images to the specific project after first image upload process.



$input['id']: holding project id value.



I do apply each() method like this:



*** code part executed after multiple file upload ***

TABLE STATUS AT FIRST STAGE :
+---------+-------------+-----------+
| id | project_id | order_id |
+---------+-------------+-----------+
| 1 | 15 | 1 |
+---------+-------------+-----------+
| 2 | 15 | 2 |
+---------+-------------+-----------+
| 3 | 16 | 1 | --> Added in the past
+---------+-------------+-----------+
| 4 | 16 | 0 | --> Created on first stage** : default 0.
+---------+-------------+-----------+
| 5 | 16 | 0 | --> Created on first stage** : default 0.
+---------+-------------+-----------+


*** code continues ***

$start = Images::select('order_id')->where('project_id', '=', $input['id'])
->orderBy('order_id', 'DESC')->first();

Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
->each(function($ord) use (&$start) {
$ord->order_id = $start++;
$ord->save();
});


Table results after the code given above executed (still in process)



+---------+-------------+-----------+
| id | project_id | order_id |
+---------+-------------+-----------+
| 1 | 15 | 1 |
+---------+-------------+-----------+
| 2 | 15 | 2 |
+---------+-------------+-----------+
| 3 | 16 | 1 | --> Added in the past
+---------+-------------+-----------+
| 4 | 16 | 0 | --> New / Present Action : 1st file
+---------+-------------+-----------+
| 5 | 16 | 0 | --> New / Present Action : 2nd one
+---------+-------------+-----------+


Expectation:



+---------+-------------+-----------+
| id | project_id | order_id |
+---------+-------------+-----------+
| 1 | 15 | 1 |
+---------+-------------+-----------+
| 2 | 15 | 2 |
+---------+-------------+-----------+
| 3 | 16 | 1 | --> Added in the past
+---------+-------------+-----------+
| 4 | 16 | 2 | --> New / Present Action : 1st file
+---------+-------------+-----------+
| 5 | 16 | 3 | --> New / Present Action : 2nd one
+---------+-------------+-----------+


What's the problem? Wrong logic or each() ? i don't know but i tried with foreach and i got same wrong results. Any help would be very appreciated. Thanks !










share|improve this question































    0















    This question is related to Eloquent save() method not working inside for loop.



    PROCESS MAP: UPLOAD FILES > CREATE TABLE ROWS WITH DEFAULT VALUE 0 > CONTINUE TO ITERATE OVER 0 PARTS(Eg If last one 3-> 4,5,6 )



    I want to continue add images to the specific project after first image upload process.



    $input['id']: holding project id value.



    I do apply each() method like this:



    *** code part executed after multiple file upload ***

    TABLE STATUS AT FIRST STAGE :
    +---------+-------------+-----------+
    | id | project_id | order_id |
    +---------+-------------+-----------+
    | 1 | 15 | 1 |
    +---------+-------------+-----------+
    | 2 | 15 | 2 |
    +---------+-------------+-----------+
    | 3 | 16 | 1 | --> Added in the past
    +---------+-------------+-----------+
    | 4 | 16 | 0 | --> Created on first stage** : default 0.
    +---------+-------------+-----------+
    | 5 | 16 | 0 | --> Created on first stage** : default 0.
    +---------+-------------+-----------+


    *** code continues ***

    $start = Images::select('order_id')->where('project_id', '=', $input['id'])
    ->orderBy('order_id', 'DESC')->first();

    Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
    ->each(function($ord) use (&$start) {
    $ord->order_id = $start++;
    $ord->save();
    });


    Table results after the code given above executed (still in process)



    +---------+-------------+-----------+
    | id | project_id | order_id |
    +---------+-------------+-----------+
    | 1 | 15 | 1 |
    +---------+-------------+-----------+
    | 2 | 15 | 2 |
    +---------+-------------+-----------+
    | 3 | 16 | 1 | --> Added in the past
    +---------+-------------+-----------+
    | 4 | 16 | 0 | --> New / Present Action : 1st file
    +---------+-------------+-----------+
    | 5 | 16 | 0 | --> New / Present Action : 2nd one
    +---------+-------------+-----------+


    Expectation:



    +---------+-------------+-----------+
    | id | project_id | order_id |
    +---------+-------------+-----------+
    | 1 | 15 | 1 |
    +---------+-------------+-----------+
    | 2 | 15 | 2 |
    +---------+-------------+-----------+
    | 3 | 16 | 1 | --> Added in the past
    +---------+-------------+-----------+
    | 4 | 16 | 2 | --> New / Present Action : 1st file
    +---------+-------------+-----------+
    | 5 | 16 | 3 | --> New / Present Action : 2nd one
    +---------+-------------+-----------+


    What's the problem? Wrong logic or each() ? i don't know but i tried with foreach and i got same wrong results. Any help would be very appreciated. Thanks !










    share|improve this question



























      0












      0








      0








      This question is related to Eloquent save() method not working inside for loop.



      PROCESS MAP: UPLOAD FILES > CREATE TABLE ROWS WITH DEFAULT VALUE 0 > CONTINUE TO ITERATE OVER 0 PARTS(Eg If last one 3-> 4,5,6 )



      I want to continue add images to the specific project after first image upload process.



      $input['id']: holding project id value.



      I do apply each() method like this:



      *** code part executed after multiple file upload ***

      TABLE STATUS AT FIRST STAGE :
      +---------+-------------+-----------+
      | id | project_id | order_id |
      +---------+-------------+-----------+
      | 1 | 15 | 1 |
      +---------+-------------+-----------+
      | 2 | 15 | 2 |
      +---------+-------------+-----------+
      | 3 | 16 | 1 | --> Added in the past
      +---------+-------------+-----------+
      | 4 | 16 | 0 | --> Created on first stage** : default 0.
      +---------+-------------+-----------+
      | 5 | 16 | 0 | --> Created on first stage** : default 0.
      +---------+-------------+-----------+


      *** code continues ***

      $start = Images::select('order_id')->where('project_id', '=', $input['id'])
      ->orderBy('order_id', 'DESC')->first();

      Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
      ->each(function($ord) use (&$start) {
      $ord->order_id = $start++;
      $ord->save();
      });


      Table results after the code given above executed (still in process)



      +---------+-------------+-----------+
      | id | project_id | order_id |
      +---------+-------------+-----------+
      | 1 | 15 | 1 |
      +---------+-------------+-----------+
      | 2 | 15 | 2 |
      +---------+-------------+-----------+
      | 3 | 16 | 1 | --> Added in the past
      +---------+-------------+-----------+
      | 4 | 16 | 0 | --> New / Present Action : 1st file
      +---------+-------------+-----------+
      | 5 | 16 | 0 | --> New / Present Action : 2nd one
      +---------+-------------+-----------+


      Expectation:



      +---------+-------------+-----------+
      | id | project_id | order_id |
      +---------+-------------+-----------+
      | 1 | 15 | 1 |
      +---------+-------------+-----------+
      | 2 | 15 | 2 |
      +---------+-------------+-----------+
      | 3 | 16 | 1 | --> Added in the past
      +---------+-------------+-----------+
      | 4 | 16 | 2 | --> New / Present Action : 1st file
      +---------+-------------+-----------+
      | 5 | 16 | 3 | --> New / Present Action : 2nd one
      +---------+-------------+-----------+


      What's the problem? Wrong logic or each() ? i don't know but i tried with foreach and i got same wrong results. Any help would be very appreciated. Thanks !










      share|improve this question
















      This question is related to Eloquent save() method not working inside for loop.



      PROCESS MAP: UPLOAD FILES > CREATE TABLE ROWS WITH DEFAULT VALUE 0 > CONTINUE TO ITERATE OVER 0 PARTS(Eg If last one 3-> 4,5,6 )



      I want to continue add images to the specific project after first image upload process.



      $input['id']: holding project id value.



      I do apply each() method like this:



      *** code part executed after multiple file upload ***

      TABLE STATUS AT FIRST STAGE :
      +---------+-------------+-----------+
      | id | project_id | order_id |
      +---------+-------------+-----------+
      | 1 | 15 | 1 |
      +---------+-------------+-----------+
      | 2 | 15 | 2 |
      +---------+-------------+-----------+
      | 3 | 16 | 1 | --> Added in the past
      +---------+-------------+-----------+
      | 4 | 16 | 0 | --> Created on first stage** : default 0.
      +---------+-------------+-----------+
      | 5 | 16 | 0 | --> Created on first stage** : default 0.
      +---------+-------------+-----------+


      *** code continues ***

      $start = Images::select('order_id')->where('project_id', '=', $input['id'])
      ->orderBy('order_id', 'DESC')->first();

      Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
      ->each(function($ord) use (&$start) {
      $ord->order_id = $start++;
      $ord->save();
      });


      Table results after the code given above executed (still in process)



      +---------+-------------+-----------+
      | id | project_id | order_id |
      +---------+-------------+-----------+
      | 1 | 15 | 1 |
      +---------+-------------+-----------+
      | 2 | 15 | 2 |
      +---------+-------------+-----------+
      | 3 | 16 | 1 | --> Added in the past
      +---------+-------------+-----------+
      | 4 | 16 | 0 | --> New / Present Action : 1st file
      +---------+-------------+-----------+
      | 5 | 16 | 0 | --> New / Present Action : 2nd one
      +---------+-------------+-----------+


      Expectation:



      +---------+-------------+-----------+
      | id | project_id | order_id |
      +---------+-------------+-----------+
      | 1 | 15 | 1 |
      +---------+-------------+-----------+
      | 2 | 15 | 2 |
      +---------+-------------+-----------+
      | 3 | 16 | 1 | --> Added in the past
      +---------+-------------+-----------+
      | 4 | 16 | 2 | --> New / Present Action : 1st file
      +---------+-------------+-----------+
      | 5 | 16 | 3 | --> New / Present Action : 2nd one
      +---------+-------------+-----------+


      What's the problem? Wrong logic or each() ? i don't know but i tried with foreach and i got same wrong results. Any help would be very appreciated. Thanks !







      laravel eloquent slim-3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 16:51







      bgul

















      asked Nov 26 '18 at 16:45









      bgulbgul

      296




      296
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Try accessing the id property when incrementing.



          Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
          ->each(function($ord) use (&$start) {
          $ord->order_id = $start->id++;
          $ord->save();
          });





          share|improve this answer
























          • Thanks for the detailed answer @adis. My stupid fault :D

            – bgul
            Nov 26 '18 at 17:09











          • No problem. Goodluck :)

            – Adis
            Nov 26 '18 at 17:09











          • Thanks again :)

            – bgul
            Nov 26 '18 at 17:23



















          1














          $start = Images::select('order_id')->where('project_id', '=', $input['id'])
          ->orderBy('order_id', 'DESC')->first();


          Returns an object of Images



          $start->order_id should contain your id-number






          share|improve this answer
























          • Hi @Artur Smolen, thanks for the important point. Now it works :D. My fault :/ :D

            – bgul
            Nov 26 '18 at 17:08












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485582%2feloquent-each-method-returns-zero-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try accessing the id property when incrementing.



          Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
          ->each(function($ord) use (&$start) {
          $ord->order_id = $start->id++;
          $ord->save();
          });





          share|improve this answer
























          • Thanks for the detailed answer @adis. My stupid fault :D

            – bgul
            Nov 26 '18 at 17:09











          • No problem. Goodluck :)

            – Adis
            Nov 26 '18 at 17:09











          • Thanks again :)

            – bgul
            Nov 26 '18 at 17:23
















          0














          Try accessing the id property when incrementing.



          Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
          ->each(function($ord) use (&$start) {
          $ord->order_id = $start->id++;
          $ord->save();
          });





          share|improve this answer
























          • Thanks for the detailed answer @adis. My stupid fault :D

            – bgul
            Nov 26 '18 at 17:09











          • No problem. Goodluck :)

            – Adis
            Nov 26 '18 at 17:09











          • Thanks again :)

            – bgul
            Nov 26 '18 at 17:23














          0












          0








          0







          Try accessing the id property when incrementing.



          Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
          ->each(function($ord) use (&$start) {
          $ord->order_id = $start->id++;
          $ord->save();
          });





          share|improve this answer













          Try accessing the id property when incrementing.



          Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
          ->each(function($ord) use (&$start) {
          $ord->order_id = $start->id++;
          $ord->save();
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 16:52









          AdisAdis

          413418




          413418













          • Thanks for the detailed answer @adis. My stupid fault :D

            – bgul
            Nov 26 '18 at 17:09











          • No problem. Goodluck :)

            – Adis
            Nov 26 '18 at 17:09











          • Thanks again :)

            – bgul
            Nov 26 '18 at 17:23



















          • Thanks for the detailed answer @adis. My stupid fault :D

            – bgul
            Nov 26 '18 at 17:09











          • No problem. Goodluck :)

            – Adis
            Nov 26 '18 at 17:09











          • Thanks again :)

            – bgul
            Nov 26 '18 at 17:23

















          Thanks for the detailed answer @adis. My stupid fault :D

          – bgul
          Nov 26 '18 at 17:09





          Thanks for the detailed answer @adis. My stupid fault :D

          – bgul
          Nov 26 '18 at 17:09













          No problem. Goodluck :)

          – Adis
          Nov 26 '18 at 17:09





          No problem. Goodluck :)

          – Adis
          Nov 26 '18 at 17:09













          Thanks again :)

          – bgul
          Nov 26 '18 at 17:23





          Thanks again :)

          – bgul
          Nov 26 '18 at 17:23













          1














          $start = Images::select('order_id')->where('project_id', '=', $input['id'])
          ->orderBy('order_id', 'DESC')->first();


          Returns an object of Images



          $start->order_id should contain your id-number






          share|improve this answer
























          • Hi @Artur Smolen, thanks for the important point. Now it works :D. My fault :/ :D

            – bgul
            Nov 26 '18 at 17:08
















          1














          $start = Images::select('order_id')->where('project_id', '=', $input['id'])
          ->orderBy('order_id', 'DESC')->first();


          Returns an object of Images



          $start->order_id should contain your id-number






          share|improve this answer
























          • Hi @Artur Smolen, thanks for the important point. Now it works :D. My fault :/ :D

            – bgul
            Nov 26 '18 at 17:08














          1












          1








          1







          $start = Images::select('order_id')->where('project_id', '=', $input['id'])
          ->orderBy('order_id', 'DESC')->first();


          Returns an object of Images



          $start->order_id should contain your id-number






          share|improve this answer













          $start = Images::select('order_id')->where('project_id', '=', $input['id'])
          ->orderBy('order_id', 'DESC')->first();


          Returns an object of Images



          $start->order_id should contain your id-number







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 16:56









          Artur SmolenArtur Smolen

          665




          665













          • Hi @Artur Smolen, thanks for the important point. Now it works :D. My fault :/ :D

            – bgul
            Nov 26 '18 at 17:08



















          • Hi @Artur Smolen, thanks for the important point. Now it works :D. My fault :/ :D

            – bgul
            Nov 26 '18 at 17:08

















          Hi @Artur Smolen, thanks for the important point. Now it works :D. My fault :/ :D

          – bgul
          Nov 26 '18 at 17:08





          Hi @Artur Smolen, thanks for the important point. Now it works :D. My fault :/ :D

          – bgul
          Nov 26 '18 at 17:08


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485582%2feloquent-each-method-returns-zero-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen