【问题标题】:Laravel : Doesn't have a default value - KeyProduct with FakerLaravel:没有默认值 - 带有 Faker 的 KeyProduct
【发布时间】:2020-09-14 20:31:57
【问题描述】:

我在创建新产品时无法添加密钥产品。 我收到错误SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into `products` (`activation_key`, `updated_at`, `created_at`) values (57394cd3-54f8-3e95-a951-e11f029fa0f5, 2020-05-27 17:09:08, 2020-05-27 17:09:08)) 我不知道为什么,它问我。

我尝试了什么:

category_id 是我在表中添加的第一列。如果我将 ->nullable() 放入 category_id ,我会得到与表中下一列相同的名称错误。

这是我的代码:

产品控制器

  public function store(Request $request)
    {

        $inputs = $request->except('_token');
        $quantity = $inputs['quantity'];
        factory(KeyProduct::class, $quantity)->create();

 foreach ($inputs as $key => $value) {
            $home->$key = $value;
        }

        $home->image=$path;
        $home->save();
        return redirect('admin/gamelist');
}

Product_table

 Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('category_id')->unsigned();
            $table->string('name');
            $table->string('image')->nullable();
            $table->string('activation_key')->nullable();
            $table->timestamps();
        });

KeyProduct_table.php

         Schema::create('key_products', function (Blueprint $table) {
           
             $table->increments('id');
             $table->string('activation_key');


             $table->timestamps();
         });

Keyproduct.php

 public function products()
    {
        return $this->HasOne('App\Product')->withPivot('quantity');
    }

产品.php

class Product extends Model
{
    public function categories()
    {
        return $this->belongsTo('App\Category', 'category_id');
    }
    
    public function keyProduct()
    {
        return $this->HasOne('App\KeyProduct');
    }

    protected $fillable = ['quantity'];
}

KeyProductFactory.php

use App\KeyProduct;
use App\Product;

$factory->define(KeyProduct::class, function (Faker $faker) {

    $product = factory(Product::class)->create();
    return [
            'activation_key' => $product->activation_key,
    
    ];
});

ProductFactory.php

use App\Product;
use Faker\Generator as Faker;

$factory->define(Product::class, function (Faker $faker) {
    return [
        'activation_key' => $faker->uuid
    ];
});

类别工厂

use App\Category;
use Faker\Generator as Faker;

$factory->define(Category::class, function (Faker $faker) {
    return [
        'activation_key' => $faker->uuid
    ];
});

感谢您的帮助。

【问题讨论】:

    标签: php database laravel join faker


    【解决方案1】:

    它失败了,因为产品是在创建时没有设置 category_id 或名称而创建的。使它们为 nullable() 或相应地更改您的创建方法。

    【讨论】:

      【解决方案2】:

      在您的 SQL 中,您仅提供了“activation_key”、“updated_at”和“created_at”列的值,因此其他字段必须至少满足一个语句:

      1. 有一个 AUTO_INCREMENT 选项;
      2. 有一个默认值;
      3. 允许 NULL 值。

      您没有提供足够的数据来完成查询。

      【讨论】:

        【解决方案3】:

        将更多$fillables 添加到您的Product.php 模型中。通过查看您的迁移,它应该如下所示:

        protected $fillable = [
            'category_id', 'name', 'image', 'activation_key', 'quantity'
        ];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-28
          • 2021-11-28
          • 1970-01-01
          • 1970-01-01
          • 2019-02-05
          • 2018-09-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多