Carbon.php 找不到分隔符 数据丢失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37957595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:26:41  来源:igfitidea点击:

Carbon.php The separation symbol could not be found Data missing

phplaraveldatelaravel-5.2php-carbon

提问by Juliver Galleto

First, I retrieve all the records,

首先,我检索所有记录,

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

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

然后我在检索到的记录上循环并修改 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));
}

but it returns me this error,

但它返回给我这个错误,

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

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

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

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

here's my model

这是我的模型

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

and in blade, I can just use

在刀片中,我可以使用

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

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

and it work just fine.

它工作得很好。

采纳答案by Ohgodwhy

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 interactswith.

我认为你这样做是错误的。数据库中的数据不需要更具人类可读性,只需要人类实际interacts使用的显示。

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

为了解决这个问题,我们将创建一个自定义accessor方法,该方法将应用于对created_at. 您可以为updated_at.

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

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

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

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 timestampcolumns should be formatted according to a specific type, such as:

如果出于某种原因您绝对需要以该格式存储的日期,那么您需要向模型添加一个属性,告诉它timestamp应该根据特定类型对列进行格式化,例如:

protected $dateFormat = 'M d, Y';

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

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