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

philipbaginski's avatar

Count age and save it into table.

Hi everyone! Next problem :-) :

In the table A I have column DOB with date: 2020-05-11. Base on this column I have to count age. As long as the age from 01.01 to 31.12 is the same I use code:

public function getAgeAttribute() { return $this->dob->startOfYear()->diffInYears(); }

And it works. Age is displayed. But I have to save this age in another table related to table A, lets say B.

There is a hidden field able to make this work, and in resource B I did a set up of request:

public function getAgeAttribute() { return $this->resourceA->dob->startOfYear()->diffInYears(); }

Hidden::make('Sample', 'sample_age')->default(function ($request) { return $request->getAgeAttribute; }),

,but it does not work.

Any idea?

0 likes
11 replies
Tray2's avatar

I see no need to store a computed value like that in your database. Just calculate it on the fly.

philipbaginski's avatar

The reason I have to store age value: The runner (with dob column) every year is older. The runner is part of event, which belongsTo eventday. The eventday has date column. In results table I have age column to save age of runner. So, after e.g. 2 years I see the age of runner he has during event. If I don't save it and will not fetch age from this column, it will show me current age of runner.

Is it a way to make it on the fly, using date (year) from eventday and date (year) from runner dob column?

pascual's avatar

I am not quite sure what you are trying to accomplish here @philipbaginski , but from what I can gather from what you and @tray2 have written so far, I have to agree with @tray2

The age of the runner can always be computed as long as you have the DOB. Or in other words, if you do not have the date of birth, you would not even be able to compute how old the runner is.

I am taking a guess here, so sorry if this is not what you have in mind, but in my head I see a view with a table listing all the runners for an event, and for each runner you see the runner's age. For this you could easily create a helper method on the runner model that returns the runners age; eg:

class Runner extends Model

function getAge()
{
	// Compute current age (eg. use Carbon)
}

If the tables listing runners for prior events should tell the user what age the runner was at the time of the event, I can see why you would want to store the age accordingly. But even for that use case I wouldn't complicate the architecture with redudant data. If you know when the event took place and you know the age of the runner, you automatically also know how old the runner was at that time. I would make a helper method for this as well and cache the results if these types of views are a busy part of your application,

philipbaginski's avatar

Yes. You see it very proper. Runner (dob) belongsTo event belongsTo eventday (date).

E.g. Runner XYZ was born in 2000. In 2018 he too part of event and:

  1. I want to store value of his age (years) in age column in results table. Saving this value makes me very sure that his age (value) will never change.
  2. Count age based on eventday (date) value and runner (dob) value on the fly. So, I don't need age column in results table. And also if I would like to create any event from the past, the age of runner will be calculated on fly.

Can you promise me, that 2nd method will always show proper value? I don't know why, but more I trust what is saved in the table than on fly counting.

Tray2's avatar

Then you calculate the DOB and the event year and you get the age of the participant at the time of the event.

philipbaginski's avatar

I use this code:

public function getAgeAttribute()
{
  $runnerDob = Runner::pluck('dob')->toArray();
  $eventDate = Event::pluck('date')->toArray();

  $from = Carbon::parse($runnerDob);
  $to = Carbon::parse($eventDate);
  $diff_in_years = $to->diffInYears($from);

  return $diff_in_years;
}

, but I have got error: DateTime::__construct() expects parameter 1 to be string, array given

Both: dob and date column is 'date' type.

Any idea how to fix it?

RamjithAp's avatar

Try this

public function getAgeAttribute()
{
  $runnerDob = $this->dob;
  $eventDate = Event::where('runner_id',$this->id)->pluck('date');

  $from = Carbon::parse($runnerDob);
  $to = Carbon::parse($eventDate);
  $diff_in_years = $to->diffInYears($from);

  return $diff_in_years;
}
philipbaginski's avatar

I think it will not work: The relation is: Runner belongsTo event belongsTo eventday.

Event has no date. So, the diff must be based only on runner table (dob) and eventday (date).

This line: $eventDate = Event::where('runner_id',$this->id)->pluck('date'); is out of connection.

RamjithAp's avatar

Then you have to tell us using which columns you have connected these tables so we can help further.

philipbaginski's avatar

The all picture:

Resource Eventday with column 'date' - set up as date. Resource Event (no column with date) Resource Runner with column 'dob' - set up as date.

Runner belongsTo Event belongsTo Eventday.

In resource Runner I have to see the age of runner.

The age of runner needs to be counted by year, but for all year: e.g. Runner was born in 2000. So, from 01.01.2020 to 31.12.2020 the age needs to be displayed as 20 (years old).

Before in old app the code was counting the age of runner based on current year and year of eventday, and was saving this value in column 'runner_age' in 'results' table. Table 'results' is storing all data about runner performances. But guys above wrote, that counting on fly is good enought.

philipbaginski's avatar

I ended up with:

public function getAgeAttribute() { $runnerDob = $this->runner->dob; $eventDate = $this->event->eventday->date;

$from = Carbon::parse($runnerDob); $to = Carbon::parse($eventDate); $diff_in_years = $to->diffInYears($from);

return $diff_in_years; }

, and it works. Is it proper way to do it?

Please or to participate in this conversation.