Php Eloquent create表示,使用Laravel 5时,列没有默认值
我有一个小API,我想保存到客户端mysql数据库中。 为了这个目的,我使用了guzzle 我的控制器:Php Eloquent create表示,使用Laravel 5时,列没有默认值,php,laravel,Php,Laravel,我有一个小API,我想保存到客户端mysql数据库中。 为了这个目的,我使用了guzzle 我的控制器: public function index() { $http = new \GuzzleHttp\Client; $res = $http->request('GET', 'http://localhost:8080/api/address'); $addresses = json_decode($res->getBod
public function index()
{
$http = new \GuzzleHttp\Client;
$res = $http->request('GET', 'http://localhost:8080/api/address');
$addresses = json_decode($res->getBody(),true);
// dd($addresses);
Address::create($addresses);
}
我的模型:
class Address extends Model
{
protected $primaryKey = 'Adresse';
protected $fillable = ['Adresse', 'Mandant', 'Kategorie', 'Matchcode', 'Name1'];
public $timestamps = false;
}
我的迁移:
public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->integer('Adresse')->primary();
$table->smallInteger('Mandant');
$table->smallInteger('Kategorie')->nullable();
$table->string('Matchcode', 50);
$table->string('Anrede', 50)->nullable();
$table->string('Name1', 50)->nullable();
});
}
我的api内容:
[
{"Adresse":"1111","Mandant":"0","Kategorie":"0","Matchcode":"fgh8881","Anrede":"Firma","Name1":"Sample name"},{"Adresse":"2399","Mandant":"0","Kategorie":"0","Matchcode":"fgh8882","Anrede":"Firma","Name1":"Sample name 1"}
]
问题是我出错了
SQLSTATE[HY000]:一般错误:1364字段“ADRESE”没有
默认值(SQL:插入地址()值())
当我将api内容限制为一个数组时,我可以毫无问题地保存它。但是如果我的api中有更多的数组,就会出现这个错误。
已设置模型上的$filleble
属性 您的API内容基本上是两条记录。对于使用eloquent的批量插入,您需要使用insert
notcreate
所以
1) 让api返回1个结果
2) 或更改地址::创建($addresses)代码>到地址::插入($addresses)代码>设置ADRESE并使用主键自动递增,您的问题将得到解决,或者如果您不想将其设置为自动递增,则将其指定为默认值 检查地址的表结构
是否有默认值NULL
需要更改默认值如果主键不是自动递增的,框架需要知道它
class Address extends Model
{
protected $primaryKey = 'Adresse';
protected $fillable = ['Adresse', 'Mandant', 'Kategorie', 'Matchcode', 'Name1'];
public $incrementing = false;
public $timestamps = false;
}
然后添加所有模型:
public function index()
{
$http = new \GuzzleHttp\Client;
$res = $http->request('GET', 'http://localhost:8080/api/address');
$addresses = json_decode($res->getBody(),true);
foreach ($addresses as $address) {
Address::create($address);
}
}
更改此行$table->integer('adrese')->primary()代码>至$table->增量('adrese')代码>我的主键不递增,这是主列i的数据类型问题guess@MuhammadNauman虽然这通常是一个很好的实践,但没有规定主键必须是autoincremetingYes@apokryfos,我应该考虑一下。我改变了它,现在我得到一个错误:类stdClass的对象无法转换为字符串
哪个解决方案,2?当我dd($addresses)
我得到这个:array:2[▼ 0 => {#253 ▼ +“地址”:“1111”+“Mandant”:“1”+“Kategorie”:“0”+“Matchcode”:“fgh8881”+“Anrede”:“公司”+“名称1”:“样本名称”}1=>{251▼ +“地址”:“2391”+“Mandant”:“1”+“Kategorie”:“0”+“Matchcode”:“fgh8882”+“Anrede”:“Firma”+“Name1”:“示例名称1”}]
您是这样解码的吗<代码>$addresses=json_解码($res->getBody(),true)代码>我现在更改了这一点,对于第二列,我得到一个错误:SQLSTATE[HY000]:一般错误:1364字段“Mandant”没有默认值(SQL:insert-into
addresses`()value())`实际上,当您上载多条记录时,其中一些记录缺少字段值,这就是为什么会发生这种情况的原因。还请将Mandant设置为null。这是一个问题,因为我需要的主键不是递增的。我无法设置$table->integer('adrese')->primary()
设置为可空或默认值您必须将Mandant
设置为不可空adrese
我已经更改了它(Mandant设置为可空),但是adrese仍然不工作,因为它是一个主键,这是一个错误点!我没有得到任何错误。但问题是我的数据库是空的。刚刚意识到你正在尝试批量插入是的。但在你的帮助下,我成功了。我将Address::create()
更改为Address::insert()
,并添加了$incrementing
属性