function in model in laravel to display age

laravel carbon
laravel datetime format
php carbon age
carbon age laravel
how to use carbon in laravel
laravel date difference
laravel carbon age calculator
how to define carbon in laravel

i create function in laravel to calculate age and display it in view but i get this error

Call to undefined method stdClass::age()

This is my model

use Carbon\Carbon;


  public function age() 
    {
    return $this->date_de_naissance->diffInYears(Carbon::now());
    }

This is my view:

 @foreach  ($patient as $pat ) 

              <tr>
                <td><a href="#" >{{ $pat->nom }}</a> </td>
                 <td> {{ $pat->prenom }} </td>
                  <td> {{ $pat->nom_epouse }} </td>
                  <td> {{ $pat->age() }}  </td>//
                  <td>{{ $pat->sexe }}</td>
                 </tr>

you can always define this code in your Model

protected $dates=['date_de_naissance'];

and then you can directly use this in your loop.

{{$pat->date_de_naissance->diffInYears(\Carbon::now())}}

instead of

{{ $pat->age() }}

Note: By defining a column name in protected array $dates, your value automatically becomes an instance of carbon while calling

Best way to do a function in Laravel to calculate age of User?, Don't call get() after find() , and don't make it static: class User extends Eloquent { public function age() { // return the user's age } } $age = User::find(1)->age();. Accessors and mutators allow you to format Eloquent attribute values when you retrieve or set them on model instances. For example, you may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model.

In case you didn't know, you can convert Date or Datetime or whatever date format to age using Carbon ->age:

Carbon::parse($pat->date_de_naissance)->age;

You need to modify $patient in controller before sending it to view because it's easier to call Carbon in controller.

foreach($patient as $pat)
    $pat['date_de_naissance'] = Carbon::parse($pat['date_de_naissance'])->age;

Carbon display age in years, months, day, Hi, I know this is more of a Carbon question rather than a Laravel question. Well. I was trying to display a user age in years, months and days (12 ye However Carbon age method only return the years. I want something more precise. @​kornel You should put it in either presenter or in the model. When a new model is saved for the first time, the creating and created events will fire. If a model already existed in the database and the save method is called, the updating / updated events will fire. However, in both cases, the saving / saved events will fire.

For now this below solved my problem

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Carbon\Carbon;
use App\Patient as pat;


class Patient extends Model
{
       use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $fillable = [
        'nom', 'id_pat', 'nom_epouse','prenom','sexe','date_de_naissance','etat_civil','id_chicha','id_tab','id_alc','id_neffa','id_autres',
    ];
    protected $table="patients";


    public static function getAge($pat) 
    {


 return Carbon::parse($pat->date_de_naissance)->diffInYears(Carbon::now());
     }

}

Calculate Age from date stored in database in Y-m-d, I would like calculate age from stored date in the database which is in this format Where to put the logic , In Controller or Model? Call to a member function diff​() on null Proudly hosted with Laravel Forge and DigitalOcean. Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.

Laravel age calculate from date using carbon, In this small tutorial, i will show you how to calculate age from date using carbon in laravel 5 application. If you work on big web application then  The filter validator, which uses PHP's filter_var function under the hood, ships with Laravel and is Laravel's pre-5.8 behavior. The dns and spoof validators require the PHP intl extension. ends_with:foo,bar, The field under validation must end with one of the given values.

Eloquent: Getting Started - Laravel, The Eloquent all method will return all of the results in the model's table. You may also pass an attribute name to see if a particular attribute was changed: static function booted() { static::addGlobalScope('age', function (Builder $builder)​  Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching.

php: How to calculate age using query builder in laravel?, you can define a function in your model like this public function age() { return $​this->dob->diffInYears(\Carbon::now()); }. after that you can call it  Eloquent ORM seems like a simple mechanism, but under the hood, there’s a lot of semi-hidden functions and less-known ways to achieve more with it. In this article, I will show you a few tricks.

Comments
  • stackoverflow.com/questions/42357941/…
  • $pat doesn't seem to be an instance of the model
  • where is this method public function age() in? is it on the model? what error did you get? are you sure passing the model? have you tried Jaydp suggestion?
  • @BagusTesa the function in the model and i get a error Call to undefined method stdClass::age()
  • and yes i tried Jaydp suggestion but no luck
  • you can always define protected $dates=['date_de_naissance']; and then you can directly use $pat->date_de_naissance->diffInYears(Carbon::now()) in your loop.