-1

enter image description here

I want to write a function that refers to that birthdate entry..

so far this is what i've done

but I get in the terminal enter image description here

TheBAST
  • 2,680
  • 10
  • 40
  • 68

2 Answers2

0

We can use below code.

$birthDate = '31-07-1983';
$data = [
    'age'=> call_user_func(function() use( $birthDate){
        return (date('Y') - date('Y', strtotime($birthDate)));
    })
];
print_r($data);
Abhijeet Jadhav
  • 188
  • 1
  • 4
0

Considering age is a calculated field and will change every day, this should not be data stored in your database, and therefore shouldn't need to be in your factory.

I'd remove the field from the database and the factory, and add an accessor to your model:

// Make sure birthdate is cast to a Carbon date.
protected $dates = [
    'birthdate',
];

// Define the "age" property accessor.
public function getAgeAttribute()
{
    return now()->diffInYears($this->birthdate);
}

With the accessor, you can access the field as a property:

$ci = App\CriminalInfo::find(1);
dd($ci->age);

You can also add it to the $appends property if you want to see it in the model's array/json output.

patricus
  • 59,488
  • 15
  • 143
  • 145