我在Laravel中创建了一个费用表单并尝试提交它以便将数据插入到数据库中,但在提交后显示此错误:
屏幕截图显示错误
C:\xampp\htdocs\Tailor\core\vendor\nesbot\carbon\src\Carbon\Carbon.php
*
* @throws \InvalidArgumentException
*
* @return static
*/
public static function createFromFormat($format, $time, $tz = null)
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
}
static::setLastErrors($lastErrors = parent::getLastErrors());
if ($dt instanceof DateTime) {
return static::instance($dt);
}
throw new InvalidArgumentException(implode(PHP_EOL, $lastErrors['errors']));
}
/**
* Set last errors.
*
* @param array $lastErrors
*
* @return void
*/
private static function setLastErrors(array $lastErrors)
{
static::$lastErrors = $lastErrors;
}
/**
* {@inheritdoc}
*/
public static function getLastErrors()
{
return static::$lastErrors;
Arguments
"Trailing data"
我已经尝试了所有可能的方式,我知道我甚至可以通过网络搜索仍然无法找到解决此问题的方法。
过去几个小时我一直在这里。
这是create.blade
<div class="portlet light bordered">
<h3 class="page-title">Expenses</h3>
{!! Form::open(['method' => 'POST', 'url' => ['admin/save']]) !!}
<div class="panel panel-default">
<div class="panel-heading">
Create
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('expenses_category_id', 'Expense Category*', ['class' => 'control-label']) !!}
{!! Form::select('expenses_category_id', $expenses_categories, old('expenses_category_id'), ['class' => 'form-control']) !!}
<p class="help-block"></p>
@if($errors->has('expenses_category_id'))
<p class="help-block">
{{ $errors->first('expenses_category_id') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('entry_date', 'Entry date*', ['class' => 'control-label']) !!}
{!! Form::text('entry_date', old('entry_date'), ['class' => 'form-control date dpicker', 'placeholder' => '']) !!}
<p class="help-block"></p>
@if($errors->has('entry_date'))
<p class="help-block">
{{ $errors->first('entry_date') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('amount', 'Amount*', ['class' => 'control-label']) !!}
{!! Form::text('amount', old('amount'), ['class' => 'form-control', 'placeholder' => '']) !!}
<p class="help-block"></p>
@if($errors->has('amount'))
<p class="help-block">
{{ $errors->first('amount') }}
</p>
@endif
</div>
</div>
</div>
</div>
{!! Form::submit('Save', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</div>
ExpensesController
public function store(StoreExpensesRequest $request)
{
Expense::create($request->all());
return redirect('admin/expenses');
}
费用模型
<?php
namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
/**
* Class Expense
*
* @package App
* @property string $expenses_category
* @property string $entry_date
* @property decimal $amount
*/
class Expense extends Model
{
use SoftDeletes;
protected $fillable = ['entry_date', 'amount', 'expenses_category_id'];
public static function boot()
{
parent::boot();
Expense::observe(new \App\Observers\UserActionsObserver);
}
/**
* Set to null if empty
* @param $input
*/
public function setExpensesCategoryIdAttribute($input)
{
$this->attributes['expenses_category_id'] = $input ? $input : null;
}/**
* Set attribute to date format
* @param $input
*/
public function setEntryDateAttribute($input)
{
if ($input != null) {
$this->attributes['entry_date'] = Carbon::createFromFormat(config('app.date_format'), $input)->format('Y-m-d');
} else {
$this->attributes['entry_date'] = null;
}
}
/**
* Get attribute from date format
* @param $input
*
* @return string
*/
public function getEntryDateAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d', $input)->format(config('app.date_format'));
} else {
return '';
}
}
public function expenses_category()
{
return $this->belongsTo(ExpensesCategory::class, 'expenses_category_id')->withTrashed();
}
}
web.php
Route::post('/save', 'ExpensesController@store');
答案 0 :(得分:0)
我不知道为什么,但是有时html表单会以...example 2020-01-01T10:10:10...
结尾的秒数返回数据,有时不是example 2020-01-01T10:10
。
为解决此问题,我制作了substr($date,0,16).":00"
,因此避免了Carbon所需的格式大小溢出。
答案 1 :(得分:0)
就我而言,我通过将以下方法添加到我的(基础)模型中来修复 Carbon 错误。
public function getDateFormat()
{
return 'Y-m-d H:i:s.u';
}
如果你在 Postgres 中使用 TIME WITH TIMEZONE 那么格式应该是:
Y-m-d H:i:s.uO