我正在开发客户端门户应用程序。 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错误的日期时间值:第1行的列
clientportal
。client_quotes
。loading_date
的'1572307200'(SQL:插入{{ 1}}(client_quotes
,first_name
,last_name
,phone
,business_name
,truck_type
,{{1} },truck_required
,quote_origin
,quote_destination
,commodity
,loading_date
)值(Ademola,Adebila,decking @ gmail.com,09004814390,JOKA,I Don不知道,2,Ajegunle,Ebutt,Rice,1572307200,2019-10-02 11:15:42,2019-10-02 11:15:42))“
我认为该错误与loading_date有关。我该如何解决?
答案 0 :(得分:0)
一个connection made: 1 sockets connected
disconncted made: 0 sockets connected
zUEZDSmfVdJnfwriAAAA disconnected
C:\Users\La Fam\desktop\project\server.js:40
if (err) throw err;
^
Error: EPERM: operation not permitted, unlink 'C:\Users\La Fam\desktop\project\u
serdatax\zUEZDSmfVdJnfwriAAAA'
列需要一个datetime
格式的值。但是您正在尝试插入Y-m-d H:i:s
之类的UNIX时间戳。因此这导致了问题。如果要使用UNIX时间戳记,请使列类型为1572307200
或保持其不变。您可以稍后将其转换为UNIX时间戳。因此,现在删除varchar
函数。
strtotime
答案 1 :(得分:0)
看着错误代码,看来loading_date属性的值是unix纪元的字符串表示形式,而不是日期时间对象。
作为修复,我建议您迁移SQL loading_date列以使用字符串值,然后将fe请求值转换为ISO dateTimeString。稍后,如果您需要使用dateTime / / Carbon对象的本机方法,则很容易将db中的字符串解析为dateTime / / Carbon对象。
这样做的另一个好处是,您还可以存储时间本地化数据,而不必担心将来对库的更改会破坏代码。