Comparing zero dating carbon object in laravel 4 blade template

I am getting a zero date from a database and converting to a carbon object in a model like this ...

protected $dates = array(
  'date',
}

      

Then in the controller I pass the object with compact ("object")

But when I show the date $ object-> in the blade template, I get '30 -11--0001 'and I want to display' None '

I tried

@if ($object->date !== '30-11--0001') $object->date else 'None' @endif

      

Does not work

@if (date('d-m-Y',strtotime($object->date)) !== '30-11--0001') $object->date else 'None' @endif

      

Does not work

@if (empty($object->date)) $object->date else 'None' @endif

      

Does not work

If I do var_dump I get

class Carbon\Carbon#491 (3) { public $date => string(27) "-0001-11-30 00:00:00.000000" public $timezone_type => int(3) public $timezone => string(13) "Europe/Madrid" }

      

Any advice? Thanks to

+3


source to share


4 answers


You have several options for this. You can control it in a given model in the base model, so it applies to all of your models, or to the presenter, which is the most appropriate place as it belongs to the presentation layer.

Note that this is not null

, but the default 0000-00-00 00:00:00

timestamp. null

the value will not be viewed this way.

1 shortest solution - Model:

public function getCreatedAtAttribute($timestamp)
{
    // flexible:
    return ( ! starts_with($timestamp, '0000')) ? $this->asDateTime($timestamp) : 'None';
    // or explicit:
    // return ($timestamp !== '0000-00-00 00:00:00') ? $this->asDateTime($timestamp) : 'None';
}

      

2 general solution is to override asDateTime

to BaseModel

(if you are using it):



protected function asDateTime($value)
{
    if (starts_with($value, '0000')) return 'None';

    return parent::asDateTime($value);
}

      

This will affect all the attributes you want to change toCarbon

.

3 presenters (I assume it just wraps model

, implementation depends on your needs):

public function createdAt()
{
    $timestamp = $this->model->created_at;

    return ( ! starts_with($timestamp, '-')) ? $timestamp : 'None';
}

      

+3


source


The problem is that the date in the database is not actually null, but it 0000-00-00 00:00:00

and Laravel will convert it to-0001-11-30 00:00:00

In this case you can use accessor - in your class you could add:

public function getCreatedAtAttribute($value)
{
    $dt = new DateTime($value);
    if (substr($dt->format('Y'),0,1) == '-') {
        return null;
    }
    return $value;
}

      



change your date value. You can just compare if the first character of the year -

and if you can returnnull

and now in your Blade template file you can use:

@if (is_null($object->created_at))
None
@else
{{ $object->created_at }}
@endif

      

+1


source


Well, this comparison might work well if you have dates> than 1970, since strtotime converts a string in the format "yyyy-mm-dd hh: mi" to a datetime type (the number of seconds since Jan 1, 1970).

$dateInvalid = "0000-00-00 00:00:00";
$dateInvalid2 = "1969-12-31 23:59:59";
$dateValid = "1970-01-01 00:00:00";
$dateValid2 = "2016-22-01 12:00:00";

return strtotime($anyDate) >= 0;

      

It may not match birth dates (<1970), but wine works for timestamps like create, update, shutdown.

+1


source


A very simple answer

@if (isset($timestamp->date) && $timestamp->date->year > 1990)
    {{ $timestamp->date->format('m/d/Y') }}
@endif

      

year is converted to -1 when running from 0000-00-00 00:00:00. So you can just show it anything> 0

0


source







All Articles