【发布时间】:2017-07-31 08:43:30
【问题描述】:
我一直在调查此问题,并找到了导致此问题的原因。查看问题底部
我在 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 列的突变器:
/**
* 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))
即使我将$genres 数组中的image 值(它是一个字符串列)设置为Anything。我也尝试像下面这样雄辩地播种:
$genre = new Genre();
$genre->name = 'Pop';
$genre->image = "anything";
$genre->status = 'PUBLISHED';
$genre->featured = 1;
$genre->save();
控制台中的错误仍然相同。有人知道吗?
更新
所以我将image 列名更改为somestring,奇怪的是播种机工作了。我尝试将image 列放在另一个表中,比如说Articles 表并在我的ArticlesTableSeeder 中添加'image' => 'test' 并且它再次起作用。然后我在 Article 模型和 BOOM 中添加了相同的 mutator!我表中的image 输出为NULL。有人知道为什么我的setImageAttribute mutator 会导致这个问题吗?
【问题讨论】:
标签: mysql laravel laravel-5 laravel-5.4 laravel-backpack