Php 用Carbon将日期保存到数据库中
我在拉雷维尔还是新手。目前,我正在学习和研究碳。我已经看过了所有的文档和其他解决方案,但我还是不明白。我希望有人能教我如何一步一步地纠正或改进我的代码 ComplaintController.phpPhp 用Carbon将日期保存到数据库中,php,laravel,date,Php,Laravel,Date,我在拉雷维尔还是新手。目前,我正在学习和研究碳。我已经看过了所有的文档和其他解决方案,但我还是不明白。我希望有人能教我如何一步一步地纠正或改进我的代码 ComplaintController.php public function store(Request $request) { if (count($request->defect_id) > 0) { foreach($request->defect_id as $item=
public function store(Request $request)
{
if (count($request->defect_id) > 0) {
foreach($request->defect_id as $item=>$v) {
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
'report_by' => Auth::user()->id,
'created_at' => Carbon::today()->toDateString(),
'updated_at' => Carbon::now()->toDateTimeString()
);
Complaint::insert($data);
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Defect Name</th>
<th>Description</th>
<th>Image</th>
<th>Report Date</th>
<th>Due Date</th>
</tr>
</thead>
@foreach($complaint as $c)
<tr>
<td>{{$c->defect->name}}</td>
<td>{{$c->description}}</td>
<td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
<td>{{$c->created_at->toDateString()}}</td>
<td></td>
</tr>
@endforeach
</table>
</div>
我将在字段中创建的保存为仅日期,没有时间,格式为(yy-mm-dd)
index.blade.php
public function store(Request $request)
{
if (count($request->defect_id) > 0) {
foreach($request->defect_id as $item=>$v) {
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
'report_by' => Auth::user()->id,
'created_at' => Carbon::today()->toDateString(),
'updated_at' => Carbon::now()->toDateTimeString()
);
Complaint::insert($data);
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Defect Name</th>
<th>Description</th>
<th>Image</th>
<th>Report Date</th>
<th>Due Date</th>
</tr>
</thead>
@foreach($complaint as $c)
<tr>
<td>{{$c->defect->name}}</td>
<td>{{$c->description}}</td>
<td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
<td>{{$c->created_at->toDateString()}}</td>
<td></td>
</tr>
@endforeach
</table>
</div>
缺陷名称
描述
形象
报告日期
到期日
@foreach(投诉为$c)
{{$c->defect->name}
{{$c->description}
image)}“class=”“alt=“{{{$c->image}}>
{{$c->created_at->toDateString()}
@endforeach
我的日期在表中仍然显示为(yy-mm-dd)
,但我希望日期的格式为(dd/mm/yy)
,因此我尝试使用这种代码{{$c->created_at->toDateString()->format(“dd/mm/yy”)}
,出现了一个错误,即调用成员函数format()稍后,我需要为duedate添加另一个字段并使用此函数addDays(30)
。那么,我需要做什么?我猜我需要在模型中添加另一个函数,但我不知道如何操作。这应该可以
要以dd/mm/yy格式保存日期,我将使用以下格式:
date('d-m-Y', strtotime(Carbon\Carbon::now()))
要在视图中重新设置日期格式,我将使用:
date('d-m-Y', strtotime($c->created_at))
我更喜欢尽可能地使用基本php函数来保持简单
如果有人知道不这样做的原因,请告诉我
至于增加天数,
date('d-m-Y', strtotime($c->created_at) + (60 * 60 * 24 * 30))
应该有用
大多数与日期相关的问题都可以用strotime()解决,它非常有用。请务必在此处阅读:这应该可以
要以dd/mm/yy格式保存日期,我将使用以下格式:
date('d-m-Y', strtotime(Carbon\Carbon::now()))
要在视图中重新设置日期格式,我将使用:
date('d-m-Y', strtotime($c->created_at))
我更喜欢尽可能地使用基本php函数来保持简单
如果有人知道不这样做的原因,请告诉我
至于增加天数,
date('d-m-Y', strtotime($c->created_at) + (60 * 60 * 24 * 30))
应该有用
大多数与日期相关的问题都可以用strotime()解决,它非常有用。请务必在此处阅读:正如@Tim Lewis所提到的,
替换
到
AstoDateString()
在更改格式之前将其转换为字符串
您还可以尝试对datetime使用标准php格式,而不是Carbon类
{{ date("d/m/Y", strtotime($c->created_at)) }}
至于第二个问题,您想添加新的表数据列
<tr>
<td>{{ $c->defect->name }}</td>
<td>{{ $c->description }}</td>
<td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
<td>{{ $c->created_at->format("dd/mm/yy") }}</td>
<td>{{ $c->created_at->addDays(30) }}</td>
</tr>
{{$c->defect->name}
{{$c->description}
image)}“class=”“alt=“{{{$c->image}}>
{{$c->创建时间->格式(“dd/mm/yy”)}
{{$c->created_at->addDays(30)}
正如@Tim Lewis所提到的,
替换
到
AstoDateString()
在更改格式之前将其转换为字符串
您还可以尝试对datetime使用标准php格式,而不是Carbon类
{{ date("d/m/Y", strtotime($c->created_at)) }}
至于第二个问题,您想添加新的表数据列
<tr>
<td>{{ $c->defect->name }}</td>
<td>{{ $c->description }}</td>
<td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
<td>{{ $c->created_at->format("dd/mm/yy") }}</td>
<td>{{ $c->created_at->addDays(30) }}</td>
</tr>
{{$c->defect->name}
{{$c->description}
image)}“class=”“alt=“{{{$c->image}}>
{{$c->创建时间->格式(“dd/mm/yy”)}
{{$c->created_at->addDays(30)}
铸造日期字段:
在Laravel中,在
处创建的和在
处更新的字段已被转换为日期字段。因此,您不必将其转换为日期格式
你可以简单地做
<td>{{ $c->created_at->format("d/m/y") }}</td>
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
'report_by' => Auth::user()->id,
);
Complaint::create($data);
额外日期字段duedate
添加自定义日期字段时,需要在模型中将其转换为日期
将此添加到您的模型中:
protected $dates = ['duedate'];
铸造日期字段:
在Laravel中,在
处创建的和在
处更新的字段已被转换为日期字段。因此,您不必将其转换为日期格式
你可以简单地做
<td>{{ $c->created_at->format("d/m/y") }}</td>
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
'report_by' => Auth::user()->id,
);
Complaint::create($data);
额外日期字段duedate
添加自定义日期字段时,需要在模型中将其转换为日期
将此添加到您的模型中:
protected $dates = ['duedate'];
我提供了另一种用法PHP date()
函数,如date(“d/m/Y”)
。你根本不用->toDateString()
将其转换为字符串。$c->created_at->format('dd/mm/yy')
应该可以正常工作。我提供了另一种用法PHP date()
函数,如date(“d/m/Y”)
。你只是不使用->toDateString()
,因为它会将其转换为字符串。$c->created_at->format('dd/mm/yy')
应该可以正常工作。“如果有人知道不使用的原因,请让我知道。”-没有理由不使用date()
overCarbon
函数,除了问题特别要求“使用Carbon”解决方案之外。此外,Carbon内置于Laravel中,因此没有理由不使用它:)您需要阅读问题@Irdina Zulkeflee想要dd/mm/yy格式,为什么这么匆忙?“如果有人知道不使用的原因,请告诉我。“-没有理由不在Carbon
函数上使用date()
,除非问题特别要求“使用Carbon”解决方案。”。另外,Carbon内置于Laravel中,因此没有理由不使用它:)您需要阅读问题@Irdina Zulkeflee想要dd/mm/yy格式,为什么这么匆忙?如果我没记错的话,Model::insert()
不会自动设置在
处创建和在
处更新,这就是它们出现在阵列中的原因<代码>模型::create()
但是:)@TimLewis是的,我认为你是对的!那么让我们使用::create()然后;-)如果我没有记错,Model::insert()
不会自动将created\u设置为
和upd