问题
The strange thing is -- all of this was working at 5.2, but I don't know what could have changed to make this happen. Below is the error and the array being inserted.
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'gender' cannot be null (SQL: insert into `tenants` (`name`, `phone`, `email`, `description`, `gender`, `date_birth`, `background_check_status`, `picture_url`, `work`, `position`, `country`, `location`, `hobbies`, `updated_at`, `created_at`) values (Amadeo Levy Luna, 18065496549, amadeo.luna@ttu.edu, , , 2017-05-08 20:29:50, 0, , , , , , , 2017-05-08 20:29:50, 2017-05-08 20:29:50)) ◀"
array:13 [▼
"_token" => "9HeacY4KskT5vpLPGCUTkzVxRcpcKMNjdob79aLs"
"name" => "Amadeo Levy Luna"
"phone" => "18065496549"
"email" => "amadeo.luna@ttu.edu"
"description" => null
"gender" => null
"background_check_status" => "0"
"picture_url" => null
"work" => null
"position" => null
"country" => null
"location" => null
"hobbies" => null
]
This is breaking in many different fields all over the website, but none of them broke before. What changed in Laravel to create this?
回答1:
Assuming nothing changed in your code the only thing I could think of is the two new middleware introduced in 5.4: TrimStrings
and ConvertEmptyStringsToNull
.
Try comment the latter or both in app\Http\Kernel.php
class Kernel extends HttpKernel
{
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
// ...
}
回答2:
Continuing on @peterm's anwser.
If you still want to convert empty strings to null
(for other parts of your application) and don't want to uncomment \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class
, you have two options.
- Add
nullable
to your column in the database. - Use events such as
saving
on your model to add a default value to your property if it isnull
.
Here is an example on how to add nullable
to your columns (if you've already created the table and if you're using mysql):
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNullToColumns extends Migration {
public function __construct() {
$this->charset = config('database.connections.mysql.charset');
$this->collate = config('database.connections.mysql.collation');
}
/**
* Run the migrations.
*
* @return void
*/
public function up() {
// Change multiple columns on a table.
DB::statement("ALTER TABLE `" . (new \App\User)->getTable() . "`
CHANGE `phone` `phone` VARCHAR(255) CHARACTER SET {$this->charset} COLLATE {$this->collate} NULL DEFAULT NULL,
CHANGE `address` `address` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NULL DEFAULT NULL,
CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NULL DEFAULT NULL;");
// Change one column on a table.
DB::statement("ALTER TABLE `" . (new \App\Report)->getTable() . "` CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset}COLLATE {$this->collate}NULL DEFAULT NULL");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
DB::statement("ALTER TABLE `" . (new \App\User)->getTable() . "`
CHANGE `phone` `phone` VARCHAR(255) CHARACTER SET {$this->charset} COLLATE {$this->collate} NOT NULL,
CHANGE `address` `address` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NOT NULL,
CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NOT NULL;");
DB::statement("ALTER TABLE `" . (new \App\Report)->getTable() . "` CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset}COLLATE {$this->collate}NOT NULL");
}
}
Here is an example on how to use events:
<?php
namespace App;
class User extends Authenticatable {
public static function boot() {
// When creating or updated the model.
static::saving(function($model){
// Use value of gender if available, if `null` use `unisex`.
$model->gender = $model->gender ?: 'unisex';
});
}
}
来源:https://stackoverflow.com/questions/43860634/laravel-5-4-upgrade-integrity-constraint-violation-column-cannot-be-null