Laravel Mutator导致SQLSTATE一般错误1364字段'x'没有默认值

时间:2017-03-10 05:38:00

标签: mysql laravel laravel-5 laravel-5.4 backpack-for-laravel

我一直在调查这个问题,我发现了导致这个问题的原因。请参阅问题的底部

我在使用Laravel 5.4并使用BackPackForLaravel作为我的管理界面。

我有一个GenreTableMigration如下:

Schema::create('genres', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('slug')->default('');
    $table->string('image');
    $table->enum('status', ['PUBLISHED', 'DRAFT'])->default('PUBLISHED');
    $table->boolean('featured')->default(0);
    $table->timestamps();
    $table->softDeletes();
});

请注意,image列不可为空,并且没有默认值。 在我的模型Genre中,这是:

protected $fillable = ['slug', 'name', 'image', 'status', 'featured'];

我的模型中还有image列的mutator:

/**
 * Store Image
 */

public function setImageAttribute($value)
{
    $attribute_name = "image";
    $disk = "public";
    $destination_path = "Albums";

    // if the image was erased
    if ($value == null) {
        // delete the image from disk
        \Storage::disk($disk)->delete($this->image);

        // set null in the database column
        $this->attributes[$attribute_name] = null;
    }

    // if a base64 was sent, store it in the db
    if (starts_with($value, 'data:image')) {
        // 0. Make the image
        $image = \Image::make($value);
        // 1. Generate a filename.
        $filename = md5($value . time()) . '.jpg';
        // 2. Store the image on disk.
        \Storage::disk($disk)->put($destination_path . '/' . $filename, $image->stream());
        // 3. Save the path to the database
        $this->attributes[$attribute_name] = $destination_path . '/' . $filename;
    }
}

我的GenresTableSeeder

$path = base_path('seeder-resources/GenresPhotos');

DB::table('genres')->delete();

$genres = [
    [
        'name' => 'Pop',
        'image' => "$path/Pop.jpeg",
        'status' => 'PUBLISHED',
        'featured' => mt_rand(0, 1),
        'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
        'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
    ],
];

foreach ($genres as $genre) {
    $this->command->info(print_r($genre));
    Genre::create($genre);
}

当我使用GenreCrudController并使用Add Genre按钮时,一切正常并且行已创建但是当我尝试播种表时,我在控制台上收到以下错误。

  [Illuminate\Database\QueryException]                                         
  SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default v  
  alue (SQL: insert into `genres` (`name`, `status`, `featured`, `created_at`  
  , `updated_at`, `slug`) values (Pop, PUBLISHED, 0, 2017-03-10 08:43:15, 201  
  7-03-10 08:43:15, pop))  

即使我将image数组中的$genres值(字符串列)设置为Anything。我也试着像下面那样雄辩地播种:

$genre = new Genre();
$genre->name = 'Pop';
$genre->image = "anything";
$genre->status = 'PUBLISHED';
$genre->featured = 1;
$genre->save();

控制台中的错误仍然相同。有没有人有任何想法?

更新 所以我已将image列名称更改为somestring,奇怪的是播种机工作了。我尝试将image列放在另一个表格中,让我们说Articles表并在我的'image' => 'test'中添加ArticlesTableSeeder,然后再次起作用。然后我在Article模型和BOOM中添加了相同的mutator!我表中的image输出为NULL。有没有人知道为什么我的setImageAttribute mutator导致了这个问题?

3 个答案:

答案 0 :(得分:0)

在您的迁移中,请更改如下并尝试:

Schema::create('genres', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->nullable();
    $table->string('slug')->nullable()->default('');
    $table->string('image')->nullable();
    $table->enum('status', ['PUBLISHED', 'DRAFT'])->default('PUBLISHED');
    $table->boolean('featured')->default(0);
    $table->timestamps();
    $table->softDeletes();
});

请在字符串字段中提供nullable()。

答案 1 :(得分:0)

首先:如果你在数据库中使用默认值,请像(可以为空)一样: $table->string('slug')->nullable()->default(''); 否则laravel不会创建正确的新记录。

第二:尝试使用'image' => $path.'/Pop.jpeg'

我不确定,但也许您必须转义字符串'image' => $path.'\/Pop.jpeg'

答案 2 :(得分:0)

所以我发现Mutator setImageAttribute只接受了Null或base64 $value,我试图播种Image File,当它失败时我试图播种一个字符串。我想出了这个Mutator而且效果很好:

/**
 * Store Image
 */
public function setImageAttribute($value)
{
    $attribute_name = "image";
    $disk = "public";
    $destination_path = "Genres";

    // if the image was erased
    if ($value == null) {
        // delete the image from disk
        \Storage::disk($disk)->delete($this->image);

        // set null in the database column
        $this->attributes[$attribute_name] = null;
    } // if a base64 was sent, store it in the db
    elseif (starts_with($value, 'data:image')) {
        // 0. Make the image
        $image = \Image::make($value);
        // 1. Generate a filename.
        $filename = md5($value . time()) . '.jpg';
        // 2. Store the image on disk.
        \Storage::disk($disk)->put($destination_path . '/' . $filename, $image->stream());
        // 3. Save the path to the database
        $this->attributes[$attribute_name] = $destination_path . '/' . $filename;
    } // else if a file was sent
    else {
        // 0. Convert the image to jpeg format
        $image = \Image::make($value)->encode('jpg', 100);
        // 1. Generate a filename
        $filename = md5($value . time()) . '.jpg';
        // 2. Store the image on disk
        \Storage::disk($disk)->put($destination_path . '/' . $filename, $image->stream());
        // 3. Save the path to databse
        $this->attributes[$attribute_name] = $destination_path . '/' . $filename;
    }
}