我想用2个字段(名称和日期)创建一个CRUD。 在我的控制器中,我想我的功能存储的日期不好吗?
public function create()
{
return view('student.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'date' => 'required'
]);
Student::create($request->all());
return redirect()->route('student.index')
->with('success', 'save');
}
这是我的文件create.blade.php
<form class="panel-body" action="{{route('student.store')}}" method="POST">
@csrf
<fieldset class="form-group">
<label for="form-group-input-1">Name</label>
<input type="text" name="name" class="form-control" id="form-group-input-1">
</fieldset>
<fieldset class="form-group">
<label for="form-group-input-1">Date</label>
<input type="text" name="date" class="form-control" id="form-group-input-1" >
</fieldset>
<a href="{{route('student.index')}}" class="btn btn-primary pull-right">Back</a>
<button type="submit" class="btn btn-sm btn-primary">Submit</button>
</form>
还有index.blade.php
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
<thead>
<tr>
<th>Firstname</th>
<th>Date</th>
</tr>
</thead>
@foreach($students as $student)
<tr>
<td> {{$student->name}}</td>
<td> {{$student->date}} </td>
<td>
<form method="POST" action="{{ route('student.destroy', $student) }} ">
<a class="btn btn-sm btn-warning" href="{{route('student.edit',$student->id)}}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
如何使“日期”字段中的创建正确?
答案 0 :(得分:0)
这是我的工作,希望对您有所帮助:
在迁移时,我的角色的类型为datetime
$table->dateTime('maintenance')->nullable();
请求规则:
'maintenance' => 'required|date_format:d-m-Y'
请求消息:
'maintenance.date_format' => ' Invalid date time ex: 01-07-2020'
我是创建功能的控制器
'maintenance' => date("Y-m-d H:i:s", strtotime($request->maintenance))
还要输入:
<input type="text" name="maintenance" value="{{ date("d-m-Y", old('maintenance') ) }}" />
我需要以d-m-Y格式阅读时间。因此,在模型中添加一个变幅器
protected $appends = ['maintenance_formated'];
//---
public function getMaintenanceFormatedAttribute()
{
return date("d-m-Y", strtotime($this->maintenance));
}