lundi 17 décembre 2018

How to get age value using birth year in laravel?

I am using Laravel 5.I have a table item with column lahiryy which consist of year of birth. I want to check the age of every persons in item table using lahiryy column. I see that can use carbon. But I am confused with the attributes.

I tried this function in item class:

 class Item extends Model 
{

protected  $primaryKey = 'ItemID';
protected $fillable = ['Name', 'Contact_Number','Address','NoOfCans','Price'];
public $timestamps = false;

public function age(){
    $YearNow = Carbon::now()->year;
    return ($YearNow->diffInYears(Carbon::parse($this->attributes['lahiryy'])));

  }
}

or using this?

  class Item extends Model 
{

protected  $primaryKey = 'ItemID';
protected $fillable = ['Name', 'Contact_Number','Address','NoOfCans','Price'];
public $timestamps = false;

public function age(){
    $years = range(Carbon::now()->year, 'lahiryy')
  }
}

I don't know how to pass in lahiryy column value in the function. I am new with carbon and laravel too. Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire