Laravel foreach month start from current monthDeleting an element from an array in PHPHow do I organise a PHP foreach loop by the filename it spits out?How does PHP 'foreach' actually work?php - for loop for each month of year3rd Last Day of the Month using PHP gmdate()Data visible only during date rangeWhy this list just go up to september?Output every month in a year in foreach loop PHPMysql date range query only check date without check month and yearAfter parsing month list sequence get changed

antimatter annihilation in stars

Complex conjugate and transpose "with respect to a basis"

Contour plot of a sequence of spheres with increasing radius

Is mountain bike good for long distances?

Strategies for dealing with chess burnout?

How to finish my PhD?

I multiply the source, you (probably) multiply the output!

Is it right to use the ideas of non-winning designers in a design contest?

I won a car in a poker game. How is that taxed in Canada?

Get Emacs to jump to the start of a word after isearch

Do you need to burn fuel between gravity assists?

How can I hint that my character isn't real?

Are professors obligated to accept supervisory role? If not, how does it work?

Did "Dirty Harry" feel lucky?

What can we do about our 9-month-old putting fingers down his throat?

Schrodinger's Cat Isn't Meant To Be Taken Seriously, Right?

Can you pop microwave popcorn on a stove?

How do you say "to hell with everything" in French?

Owner keeps cutting corners and poaching workers for his other company

Why does 8 bit truecolor use only 2 bits for blue?

How to plot two curves with the same area under?

PWM on 5V GPIO pin

Creating Chessboard with Javascript

If every star in the universe except the Sun were destroyed, would we die?



Laravel foreach month start from current month


Deleting an element from an array in PHPHow do I organise a PHP foreach loop by the filename it spits out?How does PHP 'foreach' actually work?php - for loop for each month of year3rd Last Day of the Month using PHP gmdate()Data visible only during date rangeWhy this list just go up to september?Output every month in a year in foreach loop PHPMysql date range query only check date without check month and yearAfter parsing month list sequence get changed






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have some data having date field. I want to display the data starting from the current month data ...



I get the 1st record started from current month. But next month is again starting from january. I want the records like current month+1 then currentmonth+2 and so on



@foreach($leave_manages as $holiday)
<tr>
<td class="danger">$holiday->date->format('F')</td>
<td class="warning">$holiday->date->format('d-m-Y')</td>

<td class="danger">$holiday->holiday</td>
<td class="info">$holiday->date->format('l')</td>
</tr>
@endforeach


Expected Result: March, April, May, June,............., February.



Actual Result: March, January, February..........,December










share|improve this question


























  • Can you post where the $leave_manages variable comes from. It looks like the issue is happening when you fetch this/set this variable.

    – Ken
    Mar 28 at 6:28











  • $leave_manages=Leave_manage::orderByRaw( "case when MONTH(date)=MONTH(CURDATE()) then MONTHNAME(date) end desc, MONTH(date) asc" )->get();

    – Arora
    Mar 28 at 6:32











  • Please can you show the how you're defining $leave_manages i.e. the code in your controller and/or route

    – Rwd
    Mar 28 at 7:21


















0















I have some data having date field. I want to display the data starting from the current month data ...



I get the 1st record started from current month. But next month is again starting from january. I want the records like current month+1 then currentmonth+2 and so on



@foreach($leave_manages as $holiday)
<tr>
<td class="danger">$holiday->date->format('F')</td>
<td class="warning">$holiday->date->format('d-m-Y')</td>

<td class="danger">$holiday->holiday</td>
<td class="info">$holiday->date->format('l')</td>
</tr>
@endforeach


Expected Result: March, April, May, June,............., February.



Actual Result: March, January, February..........,December










share|improve this question


























  • Can you post where the $leave_manages variable comes from. It looks like the issue is happening when you fetch this/set this variable.

    – Ken
    Mar 28 at 6:28











  • $leave_manages=Leave_manage::orderByRaw( "case when MONTH(date)=MONTH(CURDATE()) then MONTHNAME(date) end desc, MONTH(date) asc" )->get();

    – Arora
    Mar 28 at 6:32











  • Please can you show the how you're defining $leave_manages i.e. the code in your controller and/or route

    – Rwd
    Mar 28 at 7:21














0












0








0








I have some data having date field. I want to display the data starting from the current month data ...



I get the 1st record started from current month. But next month is again starting from january. I want the records like current month+1 then currentmonth+2 and so on



@foreach($leave_manages as $holiday)
<tr>
<td class="danger">$holiday->date->format('F')</td>
<td class="warning">$holiday->date->format('d-m-Y')</td>

<td class="danger">$holiday->holiday</td>
<td class="info">$holiday->date->format('l')</td>
</tr>
@endforeach


Expected Result: March, April, May, June,............., February.



Actual Result: March, January, February..........,December










share|improve this question
















I have some data having date field. I want to display the data starting from the current month data ...



I get the 1st record started from current month. But next month is again starting from january. I want the records like current month+1 then currentmonth+2 and so on



@foreach($leave_manages as $holiday)
<tr>
<td class="danger">$holiday->date->format('F')</td>
<td class="warning">$holiday->date->format('d-m-Y')</td>

<td class="danger">$holiday->holiday</td>
<td class="info">$holiday->date->format('l')</td>
</tr>
@endforeach


Expected Result: March, April, May, June,............., February.



Actual Result: March, January, February..........,December







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 7:19









Rwd

20.3k3 gold badges34 silver badges48 bronze badges




20.3k3 gold badges34 silver badges48 bronze badges










asked Mar 28 at 6:18









AroraArora

112 bronze badges




112 bronze badges















  • Can you post where the $leave_manages variable comes from. It looks like the issue is happening when you fetch this/set this variable.

    – Ken
    Mar 28 at 6:28











  • $leave_manages=Leave_manage::orderByRaw( "case when MONTH(date)=MONTH(CURDATE()) then MONTHNAME(date) end desc, MONTH(date) asc" )->get();

    – Arora
    Mar 28 at 6:32











  • Please can you show the how you're defining $leave_manages i.e. the code in your controller and/or route

    – Rwd
    Mar 28 at 7:21


















  • Can you post where the $leave_manages variable comes from. It looks like the issue is happening when you fetch this/set this variable.

    – Ken
    Mar 28 at 6:28











  • $leave_manages=Leave_manage::orderByRaw( "case when MONTH(date)=MONTH(CURDATE()) then MONTHNAME(date) end desc, MONTH(date) asc" )->get();

    – Arora
    Mar 28 at 6:32











  • Please can you show the how you're defining $leave_manages i.e. the code in your controller and/or route

    – Rwd
    Mar 28 at 7:21

















Can you post where the $leave_manages variable comes from. It looks like the issue is happening when you fetch this/set this variable.

– Ken
Mar 28 at 6:28





Can you post where the $leave_manages variable comes from. It looks like the issue is happening when you fetch this/set this variable.

– Ken
Mar 28 at 6:28













$leave_manages=Leave_manage::orderByRaw( "case when MONTH(date)=MONTH(CURDATE()) then MONTHNAME(date) end desc, MONTH(date) asc" )->get();

– Arora
Mar 28 at 6:32





$leave_manages=Leave_manage::orderByRaw( "case when MONTH(date)=MONTH(CURDATE()) then MONTHNAME(date) end desc, MONTH(date) asc" )->get();

– Arora
Mar 28 at 6:32













Please can you show the how you're defining $leave_manages i.e. the code in your controller and/or route

– Rwd
Mar 28 at 7:21






Please can you show the how you're defining $leave_manages i.e. the code in your controller and/or route

– Rwd
Mar 28 at 7:21













3 Answers
3






active

oldest

votes


















0
















Laravel provides built-in functions to get records using date functions. below code will get records from latest to older within the current year. user this



$leave_manages = $leave_manages = Leave_manage::whereYear('created_at', date('Y'))->orderBy('created_at', 'desc')->get();;






share|improve this answer


































    0
















    Since don't know the full content of your database not completely sure this is what you are looking for.



    Assuming leave_manages table contains all the months you can use native eloquent query to fetch the results



    $leave_manages = Leave_manage::whereDate('holiday', '>', CarbonCarbon::createFromDate(date('Y'), 1, 1))
    ->whereDate('holiday', '<', now())
    ->orderBy('holiday', 'acs')
    ->get();

    $leave_manages->concact(Leave_manage::whereDate('holiday', '>', now())
    ->whereDate('holiday', '<', CarbonCarbon::createFromDate(date('Y'), 12, 31))
    ->orderBy('holiday', 'acs')
    ->get());





    share|improve this answer



























    • leave_manages table has a 'date' and a 'holiday' column.. I want to display the holiday starting from the current month

      – Arora
      Mar 28 at 7:04











    • Updated the answer

      – Ken
      Mar 28 at 7:09











    • thanks a lot..but january and february records are not showing.. records displayed from march to december

      – Arora
      Mar 28 at 7:18











    • Try now()->addYear() instead of addMonths(12) (updated answer)

      – Ken
      Mar 28 at 7:23












    • I tried this code. Din't work

      – Arora
      Mar 28 at 7:27


















    0
















    $from = Carbon::now()->toDateTimeString();
    $to = Carbon::now()->subMonth(11)->toDateTimeString();
    $data = Leave_manage::where('created_at', '>=', $to)->where('created_at', '<=', $from)
    ->select(DB::raw("DATE_FORMAT(created_at, '%m') as login_month , DATE_FORMAT(created_at, '%Y') as login_year"))
    ->groupBy('login_month')
    ->groupBy('login_year')
    ->orderBy('login_year')
    ->orderBy('login_month')
    ->get();





    share|improve this answer





























      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/4.0/"u003ecc by-sa 4.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%2f55391234%2flaravel-foreach-month-start-from-current-month%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0
















      Laravel provides built-in functions to get records using date functions. below code will get records from latest to older within the current year. user this



      $leave_manages = $leave_manages = Leave_manage::whereYear('created_at', date('Y'))->orderBy('created_at', 'desc')->get();;






      share|improve this answer































        0
















        Laravel provides built-in functions to get records using date functions. below code will get records from latest to older within the current year. user this



        $leave_manages = $leave_manages = Leave_manage::whereYear('created_at', date('Y'))->orderBy('created_at', 'desc')->get();;






        share|improve this answer





























          0














          0










          0









          Laravel provides built-in functions to get records using date functions. below code will get records from latest to older within the current year. user this



          $leave_manages = $leave_manages = Leave_manage::whereYear('created_at', date('Y'))->orderBy('created_at', 'desc')->get();;






          share|improve this answer















          Laravel provides built-in functions to get records using date functions. below code will get records from latest to older within the current year. user this



          $leave_manages = $leave_manages = Leave_manage::whereYear('created_at', date('Y'))->orderBy('created_at', 'desc')->get();;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 28 at 7:39

























          answered Mar 28 at 7:28









          Adnan RasheedAdnan Rasheed

          3744 silver badges6 bronze badges




          3744 silver badges6 bronze badges


























              0
















              Since don't know the full content of your database not completely sure this is what you are looking for.



              Assuming leave_manages table contains all the months you can use native eloquent query to fetch the results



              $leave_manages = Leave_manage::whereDate('holiday', '>', CarbonCarbon::createFromDate(date('Y'), 1, 1))
              ->whereDate('holiday', '<', now())
              ->orderBy('holiday', 'acs')
              ->get();

              $leave_manages->concact(Leave_manage::whereDate('holiday', '>', now())
              ->whereDate('holiday', '<', CarbonCarbon::createFromDate(date('Y'), 12, 31))
              ->orderBy('holiday', 'acs')
              ->get());





              share|improve this answer



























              • leave_manages table has a 'date' and a 'holiday' column.. I want to display the holiday starting from the current month

                – Arora
                Mar 28 at 7:04











              • Updated the answer

                – Ken
                Mar 28 at 7:09











              • thanks a lot..but january and february records are not showing.. records displayed from march to december

                – Arora
                Mar 28 at 7:18











              • Try now()->addYear() instead of addMonths(12) (updated answer)

                – Ken
                Mar 28 at 7:23












              • I tried this code. Din't work

                – Arora
                Mar 28 at 7:27















              0
















              Since don't know the full content of your database not completely sure this is what you are looking for.



              Assuming leave_manages table contains all the months you can use native eloquent query to fetch the results



              $leave_manages = Leave_manage::whereDate('holiday', '>', CarbonCarbon::createFromDate(date('Y'), 1, 1))
              ->whereDate('holiday', '<', now())
              ->orderBy('holiday', 'acs')
              ->get();

              $leave_manages->concact(Leave_manage::whereDate('holiday', '>', now())
              ->whereDate('holiday', '<', CarbonCarbon::createFromDate(date('Y'), 12, 31))
              ->orderBy('holiday', 'acs')
              ->get());





              share|improve this answer



























              • leave_manages table has a 'date' and a 'holiday' column.. I want to display the holiday starting from the current month

                – Arora
                Mar 28 at 7:04











              • Updated the answer

                – Ken
                Mar 28 at 7:09











              • thanks a lot..but january and february records are not showing.. records displayed from march to december

                – Arora
                Mar 28 at 7:18











              • Try now()->addYear() instead of addMonths(12) (updated answer)

                – Ken
                Mar 28 at 7:23












              • I tried this code. Din't work

                – Arora
                Mar 28 at 7:27













              0














              0










              0









              Since don't know the full content of your database not completely sure this is what you are looking for.



              Assuming leave_manages table contains all the months you can use native eloquent query to fetch the results



              $leave_manages = Leave_manage::whereDate('holiday', '>', CarbonCarbon::createFromDate(date('Y'), 1, 1))
              ->whereDate('holiday', '<', now())
              ->orderBy('holiday', 'acs')
              ->get();

              $leave_manages->concact(Leave_manage::whereDate('holiday', '>', now())
              ->whereDate('holiday', '<', CarbonCarbon::createFromDate(date('Y'), 12, 31))
              ->orderBy('holiday', 'acs')
              ->get());





              share|improve this answer















              Since don't know the full content of your database not completely sure this is what you are looking for.



              Assuming leave_manages table contains all the months you can use native eloquent query to fetch the results



              $leave_manages = Leave_manage::whereDate('holiday', '>', CarbonCarbon::createFromDate(date('Y'), 1, 1))
              ->whereDate('holiday', '<', now())
              ->orderBy('holiday', 'acs')
              ->get();

              $leave_manages->concact(Leave_manage::whereDate('holiday', '>', now())
              ->whereDate('holiday', '<', CarbonCarbon::createFromDate(date('Y'), 12, 31))
              ->orderBy('holiday', 'acs')
              ->get());






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 28 at 7:56

























              answered Mar 28 at 6:42









              KenKen

              1,2893 gold badges19 silver badges28 bronze badges




              1,2893 gold badges19 silver badges28 bronze badges















              • leave_manages table has a 'date' and a 'holiday' column.. I want to display the holiday starting from the current month

                – Arora
                Mar 28 at 7:04











              • Updated the answer

                – Ken
                Mar 28 at 7:09











              • thanks a lot..but january and february records are not showing.. records displayed from march to december

                – Arora
                Mar 28 at 7:18











              • Try now()->addYear() instead of addMonths(12) (updated answer)

                – Ken
                Mar 28 at 7:23












              • I tried this code. Din't work

                – Arora
                Mar 28 at 7:27

















              • leave_manages table has a 'date' and a 'holiday' column.. I want to display the holiday starting from the current month

                – Arora
                Mar 28 at 7:04











              • Updated the answer

                – Ken
                Mar 28 at 7:09











              • thanks a lot..but january and february records are not showing.. records displayed from march to december

                – Arora
                Mar 28 at 7:18











              • Try now()->addYear() instead of addMonths(12) (updated answer)

                – Ken
                Mar 28 at 7:23












              • I tried this code. Din't work

                – Arora
                Mar 28 at 7:27
















              leave_manages table has a 'date' and a 'holiday' column.. I want to display the holiday starting from the current month

              – Arora
              Mar 28 at 7:04





              leave_manages table has a 'date' and a 'holiday' column.. I want to display the holiday starting from the current month

              – Arora
              Mar 28 at 7:04













              Updated the answer

              – Ken
              Mar 28 at 7:09





              Updated the answer

              – Ken
              Mar 28 at 7:09













              thanks a lot..but january and february records are not showing.. records displayed from march to december

              – Arora
              Mar 28 at 7:18





              thanks a lot..but january and february records are not showing.. records displayed from march to december

              – Arora
              Mar 28 at 7:18













              Try now()->addYear() instead of addMonths(12) (updated answer)

              – Ken
              Mar 28 at 7:23






              Try now()->addYear() instead of addMonths(12) (updated answer)

              – Ken
              Mar 28 at 7:23














              I tried this code. Din't work

              – Arora
              Mar 28 at 7:27





              I tried this code. Din't work

              – Arora
              Mar 28 at 7:27











              0
















              $from = Carbon::now()->toDateTimeString();
              $to = Carbon::now()->subMonth(11)->toDateTimeString();
              $data = Leave_manage::where('created_at', '>=', $to)->where('created_at', '<=', $from)
              ->select(DB::raw("DATE_FORMAT(created_at, '%m') as login_month , DATE_FORMAT(created_at, '%Y') as login_year"))
              ->groupBy('login_month')
              ->groupBy('login_year')
              ->orderBy('login_year')
              ->orderBy('login_month')
              ->get();





              share|improve this answer































                0
















                $from = Carbon::now()->toDateTimeString();
                $to = Carbon::now()->subMonth(11)->toDateTimeString();
                $data = Leave_manage::where('created_at', '>=', $to)->where('created_at', '<=', $from)
                ->select(DB::raw("DATE_FORMAT(created_at, '%m') as login_month , DATE_FORMAT(created_at, '%Y') as login_year"))
                ->groupBy('login_month')
                ->groupBy('login_year')
                ->orderBy('login_year')
                ->orderBy('login_month')
                ->get();





                share|improve this answer





























                  0














                  0










                  0









                  $from = Carbon::now()->toDateTimeString();
                  $to = Carbon::now()->subMonth(11)->toDateTimeString();
                  $data = Leave_manage::where('created_at', '>=', $to)->where('created_at', '<=', $from)
                  ->select(DB::raw("DATE_FORMAT(created_at, '%m') as login_month , DATE_FORMAT(created_at, '%Y') as login_year"))
                  ->groupBy('login_month')
                  ->groupBy('login_year')
                  ->orderBy('login_year')
                  ->orderBy('login_month')
                  ->get();





                  share|improve this answer















                  $from = Carbon::now()->toDateTimeString();
                  $to = Carbon::now()->subMonth(11)->toDateTimeString();
                  $data = Leave_manage::where('created_at', '>=', $to)->where('created_at', '<=', $from)
                  ->select(DB::raw("DATE_FORMAT(created_at, '%m') as login_month , DATE_FORMAT(created_at, '%Y') as login_year"))
                  ->groupBy('login_month')
                  ->groupBy('login_year')
                  ->orderBy('login_year')
                  ->orderBy('login_month')
                  ->get();






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 28 at 11:59

























                  answered Mar 28 at 11:48









                  ManishaManisha

                  748 bronze badges




                  748 bronze badges































                      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%2f55391234%2flaravel-foreach-month-start-from-current-month%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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript