Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

melx's avatar
Level 4

How to fetch data based on month (Jan, feb, march …Dec)

I have created button link from Jan-Dec, and i have created query which select data for the current month.

button on view blade

  <ul class="nav nav-tabs pull-left">
      <li class="active"><a href="#tab_1-1" data-toggle="tab">January</a></li>
      <li ><a href="#tab_2-2" data-toggle="tab">February</a></li>
      <li ><a href="#tab_3-3" data-toggle="tab">March</a></li>
      <li ><a href="#tab_4-4" data-toggle="tab">April</a></li>
      <li ><a href="#tab_5-5" data-toggle="tab">May</a></li>
      <li ><a href="#tab_6-6" data-toggle="tab">June</a></li>
      <li ><a href="#tab_7-7" data-toggle="tab">July</a></li>
      <li><a href="#tab_8-8" data-toggle="tab">August</a></li>
      <li ><a href="#tab_9-9" data-toggle="tab">September</a></li>
      <li ><a href="#tab_10-10" data-toggle="tab">October</a></li>   
      <li><a href="#tab_11-11" data-toggle="tab">November</a></li>
      <li ><a href="#tab_12-12" data-toggle="tab">December</a></li>
    </ul>

I want the concept when i click the tab may be January it going to execute the query of January and return data as well. the below query is execute well for current date so i want for the another month what i can add to know this query is for `jan or sept, oct.

this is what i tried it works for current only

         public function monthwisereport(){

     //check date
   $d=date('m');
       if($d==5){

 $currentdate=DB::select(DB::raw(" SELECT products.id, 
        (select categories.name from categories where categories.id=products.category_id) as category_name,

        products.name, products.open_stock as open_balance,

        (select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and MONTH(loadings.Outdate) <= MONTH(CURRENT_DATE())) as total_loadings_till_monthly,

        (select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and MONTH(stocks.Indate) <= MONTH(CURRENT_DATE())  ) as total_stocks_till_monthly,

        (select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and MONTH(loadings.Outdate) = MONTH(CURRENT_DATE())) as total_loadings_specific_date,

        (select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and MONTH(stocks.Indate) = MONTH(CURRENT_DATE())) as total_stocks_specific_date,

        ((products.open_stock)+(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and MONTH(stocks.Indate) <= MONTH(CURRENT_DATE())-1)- (select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and MONTH(loadings.Outdate) <= MONTH(CURRENT_DATE())-1)) as open_balance,

        ((products.open_stock)+(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and MONTH(stocks.Indate) <= MONTH(CURRENT_DATE()))- (select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and MONTH(loadings.Outdate) <= MONTH(CURRENT_DATE()))) as closing_balance 

        from products GROUP by products.id"));

    return view('report.monthwise',compact('currentdate'));
0 likes
2 replies
melx's avatar
Level 4

@jlrdw , its seems like this example are different with my logic i want

Please or to participate in this conversation.