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

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현