Carbon.php找不到分隔符号数据丢失 [英] Carbon.php The separation symbol could not be found Data missing

查看:87
本文介绍了Carbon.php找不到分隔符号数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我检索所有记录,

First, I retrieve all the records,

//get inventory items
$inv = inventory::all();

然后循环浏览检索到的记录,并修改created_at和updated_at数据,使其更易于阅读.

and then I loop on the retrieved records and modify the created_at and updated_at data to make it more human readable date.

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));
}

但它会向我返回此错误,

but it returns me this error,

Carbon.php第425行中的InvalidArgumentException:意外数据 成立.找到意外的数据.找不到分隔符号 数据丢失

InvalidArgumentException in Carbon.php line 425: Unexpected data found. Unexpected data found. The separation symbol could not be found Data missing

有什么想法,帮助,线索,建议,建议吗?

any ideas, help, clues, suggestions, recommendations please?

这是我的模特

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');
    }
}

在刀片中,我可以使用

{{ date("M d, Y",strtotime($i->created_at)) }}

{{ date("M d, Y",strtotime($i->updated_at)) }}

它工作正常.

推荐答案

我认为您正在以错误的方式进行操作.数据库中的数据不需要更易被人读取,只需显示人类实际interacts所使用的显示.

I think you're going about this the wrong way. The data in your database doesn't need to be more human readable, only the display that a human actually interacts with.

为解决此问题,我们将创建一个自定义的accessor方法,该方法将应用于created_at的所有调用.您可以为updated_at重新创建它.

To solve this, we will create a custom accessor method that will apply to all calls for the created_at. You can recreate this for the updated_at.

public function getCreatedAtAttribute($timestamp) {
    return Carbon\Carbon::parse($timestamp)->format('M d, Y');
}

然后,当您调用$model->created_at时,属性将以该格式返回.

Then when you call $model->created_at your attribute will return in that format.

如果出于某种原因您绝对需要使用该格式存储的日期,则需要在模型中添加一个属性,告诉它timestamp列应根据特定类型设置格式,例如:

If for some reason you absolutely need the date stored in that format, then you need to add an attribute to your model telling it that the timestamp columns should be formatted according to a specific type, such as:

protected $dateFormat = 'M d, Y';

边注
涉及到 Carbon 的原因是它应用于$table->timestamps()生成的所有列,因此created_atupdated_at列也是如此.
此外,如果您将模型中的更多列添加到protected $dates = []数组,这些列也将自动由 Carbon 处理.

Sidenote
The reason that Carbon is involved is that it is applied to all of the columns generated by $table->timestamps(), so the created_at and updated_at columns.
Furthemore, if you add more columns in the model to the protected $dates = [] array, those will also automagically be handled by Carbon.

这篇关于Carbon.php找不到分隔符号数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆