Carbon.php 找不到分隔符号 数据丢失
Carbon.php The separation symbol could not be found Data missing
首先,我检索所有记录,
//get inventory items
$inv = inventory::all();
然后我循环检索到的记录并修改 created_at 和 updated_at 数据以使其更易于阅读。
foreach($inv as $i){
$i->created_at = date("M d, Y",strtotime($i->created_at));
$i->updated_at = date("M d, Y",strtotime($i->updated_at));
}
但是 returns 我这个错误,
InvalidArgumentException in Carbon.php line 425: Unexpected data
found. Unexpected data found. The separation symbol could not be found
Data missing
有什么想法、帮助、线索、建议、建议吗?
这是我的模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class inventory extends Model
{
protected $table = "inventory";
protected $primaryKey = "item_id";
public $incrementing = false;
public function profile(){
return $this->belongsTo('App\profile','username');
}
public function inventory_images(){
return $this->hasMany('App\inventory_images','item_id');
}
}
在blade中,我可以使用
{{ date("M d, Y",strtotime($i->created_at)) }}
{{ date("M d, Y",strtotime($i->updated_at)) }}
而且效果很好。
我认为你的做法是错误的。数据库中的数据不需要 更具人类可读性,只需要人类实际 interacts
使用的显示。
为了解决这个问题,我们将创建一个自定义 accessor
方法,它将应用于 created_at
的所有调用。您可以为 updated_at
.
重新创建这个
public function getCreatedAtAttribute($timestamp) {
return Carbon\Carbon::parse($timestamp)->format('M d, Y');
}
然后当您调用 $model->created_at
时,您的属性将 return 采用该格式。
如果出于某种原因您绝对需要以该格式存储日期,那么您需要向模型添加一个属性,告诉它 timestamp
列应根据特定类型进行格式化,例如:
protected $dateFormat = 'M d, Y';
旁注
涉及 Carbon 的原因是它应用于 $table->timestamps()
生成的所有列,因此 created_at
和 updated_at
列。
此外,如果您将模型中的更多列添加到 protected $dates = []
数组,这些也会自动由 Carbon.
处理
如此简单的错误,请照做
- 检查您的日期格式 (d-m-Y),然后使用相同的格式
$shipDate = Carbon::createFromFormat('d-m-Y H:i',Carbon::parse($request->date)->format('d-m-Y') . " " . $request->time );
首先,我检索所有记录,
//get inventory items
$inv = inventory::all();
然后我循环检索到的记录并修改 created_at 和 updated_at 数据以使其更易于阅读。
foreach($inv as $i){
$i->created_at = date("M d, Y",strtotime($i->created_at));
$i->updated_at = date("M d, Y",strtotime($i->updated_at));
}
但是 returns 我这个错误,
InvalidArgumentException in Carbon.php line 425: Unexpected data found. Unexpected data found. The separation symbol could not be found Data missing
有什么想法、帮助、线索、建议、建议吗?
这是我的模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class inventory extends Model
{
protected $table = "inventory";
protected $primaryKey = "item_id";
public $incrementing = false;
public function profile(){
return $this->belongsTo('App\profile','username');
}
public function inventory_images(){
return $this->hasMany('App\inventory_images','item_id');
}
}
在blade中,我可以使用
{{ date("M d, Y",strtotime($i->created_at)) }}
{{ date("M d, Y",strtotime($i->updated_at)) }}
而且效果很好。
我认为你的做法是错误的。数据库中的数据不需要 更具人类可读性,只需要人类实际 interacts
使用的显示。
为了解决这个问题,我们将创建一个自定义 accessor
方法,它将应用于 created_at
的所有调用。您可以为 updated_at
.
public function getCreatedAtAttribute($timestamp) {
return Carbon\Carbon::parse($timestamp)->format('M d, Y');
}
然后当您调用 $model->created_at
时,您的属性将 return 采用该格式。
如果出于某种原因您绝对需要以该格式存储日期,那么您需要向模型添加一个属性,告诉它 timestamp
列应根据特定类型进行格式化,例如:
protected $dateFormat = 'M d, Y';
旁注
涉及 Carbon 的原因是它应用于 $table->timestamps()
生成的所有列,因此 created_at
和 updated_at
列。
此外,如果您将模型中的更多列添加到 protected $dates = []
数组,这些也会自动由 Carbon.
如此简单的错误,请照做
- 检查您的日期格式 (d-m-Y),然后使用相同的格式