Tuesday, April 17, 2018

Eloquent create says column has no default value using Laravel 5 - development

Eloquent create says column has no default value using 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 GuzzleHttpClient;
        $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.



from Laravel Questions and Answers https://laravelquestions.com/php/eloquent-create-says-column-has-no-default-value-using-laravel-5/
via Lzo Media

No comments:

Post a Comment