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

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.