【发布时间】:2019-10-02 11:38:23
【问题描述】:
我正在开发一个客户端门户应用程序。 Laravel-5.8 是后端,Angular-7 是前端。我有这张桌子:
表格
CREATE TABLE `client_quotes` (
`id` bigint(20) UNSIGNED NOT NULL,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`last_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`phone` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`business_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`truck_required` int(10) UNSIGNED NOT NULL,
`truck_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT 'Flatbed 20 Ton',
`quote_origin` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`quote_destination` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`commodity` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`loading_date` datetime DEFAULT NULL,
`comment` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ApiController.php
public function createClientQuote(Request $request) {
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
//'email' => 'required|email|unique:users|max:255',
'phone' => 'required|max:14',
'business_name' => 'required',
'truck_type' => 'required',
'truck_required' => 'required',
'quote_origin' => 'required',
'quote_destination' => 'required',
'commodity' => 'required',
// 'weight' => 'required',
'loading_date' => 'date|required'
]);
$clientquote = new ClientQuote;
$clientquote->first_name=$request->get('first_name');
$clientquote->last_name=$request->get('last_name');
$clientquote->email=$request->get('email');
$clientquote->phone=$request->get('phone');
$clientquote->business_name=$request->get('business_name');
$clientquote->truck_type=$request->get('truck_type');
$clientquote->truck_required=$request->get('truck_required');
$clientquote->quote_origin=$request->get('quote_origin');
$clientquote->quote_destination=$request->get('quote_destination');
$clientquote->commodity=$request->get('commodity');
$loading_date=date_create($request->get('loading_date'));
$format = date_format($loading_date,"Y-m-d H:i:s");
$clientquote->loading_date = strtotime($format);
$clientquote->save();
return response()->json([
'message' => 'Quote Successfully Sent!'
], 201);
}
在上面的 ApiController.php 中,我尝试使用这些格式设置 loading_date:
$loading_date=date_create($request->get('loading_date')); $format = date_format($loading_date,"Y-m-d H:i:s"); $clientquote->loading_date = strtotime($format);
client-quote.component.html
<form class="form-clientquote" #clientquoteForm=ngForm (ngSubmit)="onSubmit()">
<div class="row">
<div class="col-xs-12">
<div class="col-xs-6">
<label for="loading_date">Loading Date<span style="color:red;"> *</span></label>
<div class="input-group date" style="width: 100%;" >
<mat-form-field>
<input matInput [matDatepicker] = "picker" placeholder = "Choose a date" name="loading_date" [ngModel]="form.loading_date | date: 'dd/MM/yyyy'" (ngModelChange)=" form.loading_date= $event" #loading_date="ngModel" [ngClass]="{'is-invalid' : loading_date.invalid && ((loading_date.dirty || loading_date.touched) || clientquoteForm.submitted)}" required>
<mat-datepicker-toggle matSuffix [for] = "picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<div class="form-feedback" *ngIf="loading_date.invalid && ((loading_date.dirty || loading_date.touched) || clientquoteForm.submitted)">
<div style="color:red;" *ngIf="loading_date.errors?.required"class="alert alert-danger">Loading Date is required.</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="col-xs-12">
<div class="btn-group">
<button style="margin:5px" (keyup.enter)="onSubmit()" type="submit" class="btn btn-success" awNextStep> Get A Quote</button>
</div>
</div>
</div>
</div>
</form>
当我点击提交按钮时,我希望应用程序保存到数据库中。我收到了这个错误:
"SQLSTATE[22007]: 无效的日期时间格式: 1292 错误的日期时间值: '1572307200' 列
clientportal.client_quotes.loading_date在第 1 行 (SQL: 插入到client_quotes(first_name,last_name,phone,business_name,truck_type,truck_required,quote_origin,quote_destination,commodity,commodity,updated_at,loading_date, @98765434 Ademola, Adebila, decking@gmail.com, 09048614390, JOKA, I Don't know, 2, Ajegunle, Ebutt, Rice, 1572307200, 2019-10-02 11:15:42, 2019-10-02 11:15: 42))"
我认为错误与加载日期有关。我该如何解决?
【问题讨论】:
-
尝试转储
$request->get('loading_date')和date_format($loading_date,"Y-m-d H:i:s")和strtotime($format)以查看日期格式。它似乎在某处转换为时间戳