Laravel working with dynamic start and end dates to grab some data












0












$begingroup$


So, to start off, the below code does work, but it feels like a newbie solution to me, and if I ever need to come back to this code, I will have no idea what I was thinking.



I have a table called incomes with a model Income, and one of the columns is called description_id. If the description_id matches a certain Description::id, I return some dates.



Example:



I have a monthly_overview view, where I show all expenses for a given month and year. So for example, mydomain/overview/monthly/2018-12. Based on this given date, I need to figure out the start_date and end_date, so I can get all expenses from a given range.



How do I determine these dates? Based on the incomes table as described above. Let's say, I have these incomes in my database:



Income #1    date: 2018-10-23    description_id: 5
Income #2 date: 2018-11-22 description_id: 5
Income #3 date: 2018-11-21 description_id: 5
Income #4 date: 2019-01-10 description_id: 2
Income #5 date: 2019-01-23 description_id: 5


description_id 5 means it is a monthly salary, so we need to generate the start_date and end_date based on these. Income #4 must be excluded in this case.



So, for example, I want to generate the monthly overview for december. I pass 2018-12 to my controller. I currently have the following method:



private function getIncome()
{
return auth()->user()->incomes()->where('description_id', settings('income_description'))
->whereMonth('date', '=', $this->date->format('m'))
->whereYear('date', '=', $this->date->format('Y'))
->first();
}


settings('income_description') simply returns 5 in this case.



$date in this case is basically a Carbon date, created from the year and month that were passed to the controller:



Carbon::createFromDate($year, $month, null);


So, when I need to get the exact start_date for the give month and year, I have another method:



private function getStartDate()
{
if (!$this->getIncome())
{
return startDate(); // This simply returns the user registration date, if no income with the given `description_id` was found
}

return $this->getIncome()->date;
}


So far this all seems like the best way to go for me. But if there are any "tips" on how to improve what I have so far, please feel free.



The "issue" I have is with determining the end_date. I need to perform some checks. First, check if $this->getIncome() is not null. If it is null, I need to add one month to $this->getStartDate() to determine the end_date.



if (!$this->getIncome())
{
return $this->startDatePlusMonth();
}


Next, if $this->getIncome() was not null, I need to check if a newer Income can be found with the correct description_id, to determine the end_date like that.



$date = auth()->user()->incomes()
->where('description_id', settings('income_description'))
->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');


So, if nothing was found, we return



if ($date == null)
{
return $this->startDatePlusMonth();
}

return $date;


And here is my startDatePlusMonth method.



private function startDatePlusMonth()
{
return Carbon::createFromFormat("Y-m-d", $this->getStartDate())->addMonth(1)->format('Y-m-d');
}


Something like the below would already be a nicer way of doing things, but if $this->getIncome() is null, I will get errors ofcourse.



private function getEndDate()
{
$date = auth()->user()->incomes()
->where('description_id', settings('income_description'))
->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');

if (!$this->getIncome() && $date == null)
{
$this->startDatePlusMonth();
}

return $date;
}


My ultimate goal is to learn new things, and improve my code ofcourse. So anything you can give me that can improve this code and help me learn some new stuff, is greatly appreciated.









share









$endgroup$

















    0












    $begingroup$


    So, to start off, the below code does work, but it feels like a newbie solution to me, and if I ever need to come back to this code, I will have no idea what I was thinking.



    I have a table called incomes with a model Income, and one of the columns is called description_id. If the description_id matches a certain Description::id, I return some dates.



    Example:



    I have a monthly_overview view, where I show all expenses for a given month and year. So for example, mydomain/overview/monthly/2018-12. Based on this given date, I need to figure out the start_date and end_date, so I can get all expenses from a given range.



    How do I determine these dates? Based on the incomes table as described above. Let's say, I have these incomes in my database:



    Income #1    date: 2018-10-23    description_id: 5
    Income #2 date: 2018-11-22 description_id: 5
    Income #3 date: 2018-11-21 description_id: 5
    Income #4 date: 2019-01-10 description_id: 2
    Income #5 date: 2019-01-23 description_id: 5


    description_id 5 means it is a monthly salary, so we need to generate the start_date and end_date based on these. Income #4 must be excluded in this case.



    So, for example, I want to generate the monthly overview for december. I pass 2018-12 to my controller. I currently have the following method:



    private function getIncome()
    {
    return auth()->user()->incomes()->where('description_id', settings('income_description'))
    ->whereMonth('date', '=', $this->date->format('m'))
    ->whereYear('date', '=', $this->date->format('Y'))
    ->first();
    }


    settings('income_description') simply returns 5 in this case.



    $date in this case is basically a Carbon date, created from the year and month that were passed to the controller:



    Carbon::createFromDate($year, $month, null);


    So, when I need to get the exact start_date for the give month and year, I have another method:



    private function getStartDate()
    {
    if (!$this->getIncome())
    {
    return startDate(); // This simply returns the user registration date, if no income with the given `description_id` was found
    }

    return $this->getIncome()->date;
    }


    So far this all seems like the best way to go for me. But if there are any "tips" on how to improve what I have so far, please feel free.



    The "issue" I have is with determining the end_date. I need to perform some checks. First, check if $this->getIncome() is not null. If it is null, I need to add one month to $this->getStartDate() to determine the end_date.



    if (!$this->getIncome())
    {
    return $this->startDatePlusMonth();
    }


    Next, if $this->getIncome() was not null, I need to check if a newer Income can be found with the correct description_id, to determine the end_date like that.



    $date = auth()->user()->incomes()
    ->where('description_id', settings('income_description'))
    ->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');


    So, if nothing was found, we return



    if ($date == null)
    {
    return $this->startDatePlusMonth();
    }

    return $date;


    And here is my startDatePlusMonth method.



    private function startDatePlusMonth()
    {
    return Carbon::createFromFormat("Y-m-d", $this->getStartDate())->addMonth(1)->format('Y-m-d');
    }


    Something like the below would already be a nicer way of doing things, but if $this->getIncome() is null, I will get errors ofcourse.



    private function getEndDate()
    {
    $date = auth()->user()->incomes()
    ->where('description_id', settings('income_description'))
    ->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');

    if (!$this->getIncome() && $date == null)
    {
    $this->startDatePlusMonth();
    }

    return $date;
    }


    My ultimate goal is to learn new things, and improve my code ofcourse. So anything you can give me that can improve this code and help me learn some new stuff, is greatly appreciated.









    share









    $endgroup$















      0












      0








      0





      $begingroup$


      So, to start off, the below code does work, but it feels like a newbie solution to me, and if I ever need to come back to this code, I will have no idea what I was thinking.



      I have a table called incomes with a model Income, and one of the columns is called description_id. If the description_id matches a certain Description::id, I return some dates.



      Example:



      I have a monthly_overview view, where I show all expenses for a given month and year. So for example, mydomain/overview/monthly/2018-12. Based on this given date, I need to figure out the start_date and end_date, so I can get all expenses from a given range.



      How do I determine these dates? Based on the incomes table as described above. Let's say, I have these incomes in my database:



      Income #1    date: 2018-10-23    description_id: 5
      Income #2 date: 2018-11-22 description_id: 5
      Income #3 date: 2018-11-21 description_id: 5
      Income #4 date: 2019-01-10 description_id: 2
      Income #5 date: 2019-01-23 description_id: 5


      description_id 5 means it is a monthly salary, so we need to generate the start_date and end_date based on these. Income #4 must be excluded in this case.



      So, for example, I want to generate the monthly overview for december. I pass 2018-12 to my controller. I currently have the following method:



      private function getIncome()
      {
      return auth()->user()->incomes()->where('description_id', settings('income_description'))
      ->whereMonth('date', '=', $this->date->format('m'))
      ->whereYear('date', '=', $this->date->format('Y'))
      ->first();
      }


      settings('income_description') simply returns 5 in this case.



      $date in this case is basically a Carbon date, created from the year and month that were passed to the controller:



      Carbon::createFromDate($year, $month, null);


      So, when I need to get the exact start_date for the give month and year, I have another method:



      private function getStartDate()
      {
      if (!$this->getIncome())
      {
      return startDate(); // This simply returns the user registration date, if no income with the given `description_id` was found
      }

      return $this->getIncome()->date;
      }


      So far this all seems like the best way to go for me. But if there are any "tips" on how to improve what I have so far, please feel free.



      The "issue" I have is with determining the end_date. I need to perform some checks. First, check if $this->getIncome() is not null. If it is null, I need to add one month to $this->getStartDate() to determine the end_date.



      if (!$this->getIncome())
      {
      return $this->startDatePlusMonth();
      }


      Next, if $this->getIncome() was not null, I need to check if a newer Income can be found with the correct description_id, to determine the end_date like that.



      $date = auth()->user()->incomes()
      ->where('description_id', settings('income_description'))
      ->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');


      So, if nothing was found, we return



      if ($date == null)
      {
      return $this->startDatePlusMonth();
      }

      return $date;


      And here is my startDatePlusMonth method.



      private function startDatePlusMonth()
      {
      return Carbon::createFromFormat("Y-m-d", $this->getStartDate())->addMonth(1)->format('Y-m-d');
      }


      Something like the below would already be a nicer way of doing things, but if $this->getIncome() is null, I will get errors ofcourse.



      private function getEndDate()
      {
      $date = auth()->user()->incomes()
      ->where('description_id', settings('income_description'))
      ->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');

      if (!$this->getIncome() && $date == null)
      {
      $this->startDatePlusMonth();
      }

      return $date;
      }


      My ultimate goal is to learn new things, and improve my code ofcourse. So anything you can give me that can improve this code and help me learn some new stuff, is greatly appreciated.









      share









      $endgroup$




      So, to start off, the below code does work, but it feels like a newbie solution to me, and if I ever need to come back to this code, I will have no idea what I was thinking.



      I have a table called incomes with a model Income, and one of the columns is called description_id. If the description_id matches a certain Description::id, I return some dates.



      Example:



      I have a monthly_overview view, where I show all expenses for a given month and year. So for example, mydomain/overview/monthly/2018-12. Based on this given date, I need to figure out the start_date and end_date, so I can get all expenses from a given range.



      How do I determine these dates? Based on the incomes table as described above. Let's say, I have these incomes in my database:



      Income #1    date: 2018-10-23    description_id: 5
      Income #2 date: 2018-11-22 description_id: 5
      Income #3 date: 2018-11-21 description_id: 5
      Income #4 date: 2019-01-10 description_id: 2
      Income #5 date: 2019-01-23 description_id: 5


      description_id 5 means it is a monthly salary, so we need to generate the start_date and end_date based on these. Income #4 must be excluded in this case.



      So, for example, I want to generate the monthly overview for december. I pass 2018-12 to my controller. I currently have the following method:



      private function getIncome()
      {
      return auth()->user()->incomes()->where('description_id', settings('income_description'))
      ->whereMonth('date', '=', $this->date->format('m'))
      ->whereYear('date', '=', $this->date->format('Y'))
      ->first();
      }


      settings('income_description') simply returns 5 in this case.



      $date in this case is basically a Carbon date, created from the year and month that were passed to the controller:



      Carbon::createFromDate($year, $month, null);


      So, when I need to get the exact start_date for the give month and year, I have another method:



      private function getStartDate()
      {
      if (!$this->getIncome())
      {
      return startDate(); // This simply returns the user registration date, if no income with the given `description_id` was found
      }

      return $this->getIncome()->date;
      }


      So far this all seems like the best way to go for me. But if there are any "tips" on how to improve what I have so far, please feel free.



      The "issue" I have is with determining the end_date. I need to perform some checks. First, check if $this->getIncome() is not null. If it is null, I need to add one month to $this->getStartDate() to determine the end_date.



      if (!$this->getIncome())
      {
      return $this->startDatePlusMonth();
      }


      Next, if $this->getIncome() was not null, I need to check if a newer Income can be found with the correct description_id, to determine the end_date like that.



      $date = auth()->user()->incomes()
      ->where('description_id', settings('income_description'))
      ->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');


      So, if nothing was found, we return



      if ($date == null)
      {
      return $this->startDatePlusMonth();
      }

      return $date;


      And here is my startDatePlusMonth method.



      private function startDatePlusMonth()
      {
      return Carbon::createFromFormat("Y-m-d", $this->getStartDate())->addMonth(1)->format('Y-m-d');
      }


      Something like the below would already be a nicer way of doing things, but if $this->getIncome() is null, I will get errors ofcourse.



      private function getEndDate()
      {
      $date = auth()->user()->incomes()
      ->where('description_id', settings('income_description'))
      ->where('date', '>', $this->getIncome()->date)->limit(1)->value('date');

      if (!$this->getIncome() && $date == null)
      {
      $this->startDatePlusMonth();
      }

      return $date;
      }


      My ultimate goal is to learn new things, and improve my code ofcourse. So anything you can give me that can improve this code and help me learn some new stuff, is greatly appreciated.







      php datetime laravel interval





      share












      share










      share



      share










      asked 7 mins ago









      HardistHardist

      2121213




      2121213






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f212196%2flaravel-working-with-dynamic-start-and-end-dates-to-grab-some-data%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f212196%2flaravel-working-with-dynamic-start-and-end-dates-to-grab-some-data%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python