gpt4 book ai didi

mysql - SQLSTATE[HY000] : General error: 1364 Field 'name' doesn't have a default value, Laravel

转载 作者:行者123 更新时间:2023-11-29 09:57:22 25 4
gpt4 key购买 nike

嗨,我刚刚接触 API 制作和使用,我使用 Laravel 作为 api 部分,并使用 Angular 的 HttpClient 来发出请求,这是代码

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';


@Injectable()
export class ApitestProvider {
tests;
apiUrl = "http://127.0.0.1:8000/api";
testAdded;
testGotten;
testDeleted;
constructor(public http: HttpClient) {

console.log('Hello ApitestProvider Provider');
}

addTest(data) {
/*let nData= JSON.stringify(data);
console.log(nData);*/
let obj = {name : "test"};
this.http.post(this.apiUrl + "/tests",JSON.stringify(obj))
.subscribe(res => {
console.log(res);
this.testAdded = res;
}, (err) => {
console.log(err);
});
}

出于测试目的修改了代码,这是我的 Controller 的 Laravel 代码

public function store(Request $request){
return Testme::create($request->all());
}

这是初始迁移的代码

class CreateTestmesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('testmes', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('testmes');
}
}

此外,在模型中,唯一可填写的是“名称”,提前致谢。

最佳答案

我发现了这个问题,我只需在我发送的请求中设置数据格式的 header

只需使用 httpheaders

  addTest(data) {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');
let nData = JSON.stringify(data);
console.log(nData)
this.http.post(this.apiUrl + "/tests", nData, {headers: headers})
.subscribe(res => {
console.log(res);
this.testAdded = res;
}, (err) => {
console.log(err);
});
}

关于mysql - SQLSTATE[HY000] : General error: 1364 Field 'name' doesn't have a default value, Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53616786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com