Corbon的问题

照着视频上面敲的代码,这个问题是出在哪里呢

public function setPublishedAtAttribute($date)
{    $this->attributes['published_at']=Carbon::createFromFormat('Y-m-d',$date);
}

error:
InvalidArgumentException in Carbon.php line 425: Trailing data

出现这个原因可能是前端过来的格式不太一样:

  {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!} 

两个方法,试试

第一:

$this->attributes['published_at'] = new Carbon($date);

第二:

Carbon::createFromFormat('d-m-Y', $date);
JellyBool

出现这个原因可能是前端过来的格式不太一样:

  {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!} 

两个方法,试试

第一:

$this->attributes['published_at'] = new Carbon($date);

第二:

Carbon::createFromFormat('d-m-Y', $date);
Mango

setAttribute 如果前端传来的格式不一样。 就error.有办法优化吗?
例:
public function setManageFeeAttribute($managefee)
{
this>attributes[managefee]=Carbon::createFromFormat(Y.n,this->attributes['managefee']=Carbon::createFromFormat('Y.n',managefee)->startOfMonth();
}
限定前端传来的日期格式是 2016.3

目前是如果用户传空值或者格式不对。 直接报错Trailing data。
用户拿不到提示信息。So.有什么好办法优化吗?

Mango

咦,我印象原来社区是按最近回复排序的,现在好像是按发帖时间排序。。
沉底贴,召唤一下。
@JellyBool

JellyBool

没记错是没有的,你可以先保存的一样的格式,在显示的时候在 format就可以了 @Mango