Laravel - Carbon 意外数据
Laravel - Carbon unexpected data
我用 Laravel 开始了一个小项目。
一切正常,只是不适用于 Firefox。
我总是收到错误消息,即碳获取了异常数据。
但在所有其他浏览器中它都有效。
我试图将输入字段 "data" 更改为 deadline_date
进入 "text" 并再次收到消息。如果我停用 "deadline_date" 一切工作的段落。
控制器
public function create(StoreMilestone $request)
{
Milestone::create([
'name' => $request->input('name'),
'deadline_date' => $request->input('deadline'),
'description' => $request->input('description')
]);
return redirect()->back();
}
验证
public function rules()
{
return [
'name' => 'required',
'deadline' => 'required|date',
'description' => 'required'
];
}
型号
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Milestone extends Model
{
use SoftDeletes;
public $timestamps = false;
protected $table = 'fairkatert_milestone';
protected $fillable = [ 'name', 'deadline_date', 'description' ];
protected $dates = [ 'deadline_date' ];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function getTasks()
{
return $this->belongsToMany(
'App\Http\Models\Task',
'fairkatert_task_assign_milestone'
);
}
/**
* @return mixed
*/
public function getCloseTasks()
{
return $this->getTasks()
->where('status', 'close')
->count();
}
/**
* @return mixed
*/
public function getOpenTasks()
{
return $this->getTasks()
->where('status', 'open')
->count();
}
/**
* @return string
*/
public function getMilestoneProgress()
{
$total = $this->getTasks()->count();
$close = $this->getCloseTasks();
if($total === 0)
{
$result = 0;
}
else
{
$result = 100 * $close / $total;
}
return number_format($result, 2, '.', '');
}
/**
* @return mixed
*/
public function getUnorderedTasks()
{
$task = Task::where([
'ordered' => false
])->get();
return $task;
}
}
表格
<form method="post" >
<label>Name</label>
<input type="text" class="form-control input-sm" name="name" value="{{ milestone.name }}" placeholder="Milestone">
<label>Deadline</label>
<input type="date" class="form-control input-sm" name="deadline" value="{{ milestone.deadline_date|date('d.m.Y') }}" placeholder="Datum">
<label>Beschreibung</label>
<textarea id="mytextarea" class="form-control input-sm" name="description" rows="5" placeholder="Beschreibung">{{ milestone.description }}</textarea>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{% if edit == true %}
<button type="submit" formaction="{{ url('milestone/' ~ milestone.id ~ '/edit') }}" class="btn btn-sm btn-success margin-t-5 p">
<i class="fa fa-floppy-o"></i> Milestone bearbeiten
</button>
{% else %}
<button type="submit" formaction="{{ url('milestone/create') }}" class="btn btn-sm btn-success margin-t-5 p">
<i class="fa fa-floppy-o"></i> Milestone speichern
</button>
{% endif %}
根据您的 link http://imgur.com/h5d8phO
如果您的日期格式是 d-m-Y
那么
Carbon::createFromFormat('Y-m-d', $date);
不会工作,因为您没有指定正确的格式;你需要
Carbon::createFromFormat('d-m-Y', $date);
您指定为值的日期格式的格式必须与要转换为 Carbon 对象的日期字符串的实际格式相匹配。
我用 Laravel 开始了一个小项目。 一切正常,只是不适用于 Firefox。 我总是收到错误消息,即碳获取了异常数据。 但在所有其他浏览器中它都有效。
我试图将输入字段 "data" 更改为 deadline_date 进入 "text" 并再次收到消息。如果我停用 "deadline_date" 一切工作的段落。
控制器
public function create(StoreMilestone $request)
{
Milestone::create([
'name' => $request->input('name'),
'deadline_date' => $request->input('deadline'),
'description' => $request->input('description')
]);
return redirect()->back();
}
验证
public function rules()
{
return [
'name' => 'required',
'deadline' => 'required|date',
'description' => 'required'
];
}
型号
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Milestone extends Model
{
use SoftDeletes;
public $timestamps = false;
protected $table = 'fairkatert_milestone';
protected $fillable = [ 'name', 'deadline_date', 'description' ];
protected $dates = [ 'deadline_date' ];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function getTasks()
{
return $this->belongsToMany(
'App\Http\Models\Task',
'fairkatert_task_assign_milestone'
);
}
/**
* @return mixed
*/
public function getCloseTasks()
{
return $this->getTasks()
->where('status', 'close')
->count();
}
/**
* @return mixed
*/
public function getOpenTasks()
{
return $this->getTasks()
->where('status', 'open')
->count();
}
/**
* @return string
*/
public function getMilestoneProgress()
{
$total = $this->getTasks()->count();
$close = $this->getCloseTasks();
if($total === 0)
{
$result = 0;
}
else
{
$result = 100 * $close / $total;
}
return number_format($result, 2, '.', '');
}
/**
* @return mixed
*/
public function getUnorderedTasks()
{
$task = Task::where([
'ordered' => false
])->get();
return $task;
}
}
表格
<form method="post" >
<label>Name</label>
<input type="text" class="form-control input-sm" name="name" value="{{ milestone.name }}" placeholder="Milestone">
<label>Deadline</label>
<input type="date" class="form-control input-sm" name="deadline" value="{{ milestone.deadline_date|date('d.m.Y') }}" placeholder="Datum">
<label>Beschreibung</label>
<textarea id="mytextarea" class="form-control input-sm" name="description" rows="5" placeholder="Beschreibung">{{ milestone.description }}</textarea>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{% if edit == true %}
<button type="submit" formaction="{{ url('milestone/' ~ milestone.id ~ '/edit') }}" class="btn btn-sm btn-success margin-t-5 p">
<i class="fa fa-floppy-o"></i> Milestone bearbeiten
</button>
{% else %}
<button type="submit" formaction="{{ url('milestone/create') }}" class="btn btn-sm btn-success margin-t-5 p">
<i class="fa fa-floppy-o"></i> Milestone speichern
</button>
{% endif %}
根据您的 link http://imgur.com/h5d8phO
如果您的日期格式是 d-m-Y
那么
Carbon::createFromFormat('Y-m-d', $date);
不会工作,因为您没有指定正确的格式;你需要
Carbon::createFromFormat('d-m-Y', $date);
您指定为值的日期格式的格式必须与要转换为 Carbon 对象的日期字符串的实际格式相匹配。