dongyinju5977
dongyinju5977
2018-04-17 06:46

Eloquent create表示列使用Laravel 5没有默认值

已采纳

I have an small API that i want to save into client mysql database. For this purpose i'm using guzzle.

my controller:

 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);
    }

my model:

class Address extends Model
{
    protected $primaryKey = 'Adresse';
    protected $fillable = ['Adresse', 'Mandant', 'Kategorie', 'Matchcode', 'Name1'];

    public $timestamps = false;

}

my migration:

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();
   });
}

my api content:

[
{"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"}
]

the problem is i get an error

SQLSTATE[HY000]: General error: 1364 Field 'Adresse' doesn't have a default value (SQL: insert into addresses () values ())

when i limit the api content to one array i can save it without a problem. But if i have more arrays in my api i get this error. $fillable property on the model is set.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • dsxsou8465 dsxsou8465 3年前

    If your primary key is not auto-incrementing the framework needs to know about it.

    class Address extends Model
    {
        protected $primaryKey = 'Adresse';
        protected $fillable = ['Adresse', 'Mandant', 'Kategorie', 'Matchcode', 'Name1'];
        public $incrementing = false;
    
        public $timestamps = false;
    
    }
    

    Then to add all of the models:

    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);
        }
    }
    
    点赞 评论 复制链接分享
  • doutuan6158 doutuan6158 3年前

    check the table structure of addresses whether it have default value NULL need to change default

    点赞 评论 复制链接分享
  • dongzhan1492 dongzhan1492 3年前

    Set Adresse be auto-increment as well with primary key and your issue will be solved or If you do not want to set it to auto-increment then assign it to a default value.

    点赞 评论 复制链接分享
  • duanjiao6711 duanjiao6711 3年前

    Your API content is essentially 2 records. For mass inserting using eloquent you need to use insert not create

    So either

    1) have your api return 1 result

    2) or change Address::create($addresses); to Address::insert($addresses);

    点赞 评论 复制链接分享