亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Laravel 9.x中的常規(guī)錯(cuò)誤:字段'id'在沒有預(yù)設(shè)值的情況下
P粉714780768
P粉714780768 2024-04-06 11:12:54
0
1
806

我在我的應(yīng)用程式中使用 UUID,並且我已經(jīng)實(shí)現(xiàn)了該特徵,如在線所示,如下所示:

trait Uuid
{
    protected static function boot(): void
    {
        parent::boot();

        static::creating(function (Model $model) {
            if (!$model->getKey()) {
                $model->{$model->getKeyName()} = (string) Str::uuid();
            }
        });
    }

    public function getIncrementing(): bool
    {
        return false;
    }

    public function getKeyType(): string
    {
        return 'string';
    }
}

回想起來,這幾乎適用於任何地方:我正在嘗試在我的產(chǎn)品上建立資料透視表,如下所示:

public function categories(): BelongsToMany
{
    return $this->belongsToMany(
        Category::class,
        ProductCategory::class,
        'product_id',
        'category_id'
    );
}

遷移如下圖所示:

public function up(): void
{
    Schema::create('product_categories', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->foreignUuid('product_id')->index();
        $table->foreignUuid('category_id')->index();
        $table->timestamps();
    });
}

但是,每當(dāng)我在播種時(shí)執(zhí)行以下操作:

Product::first()->categories()->sync(Categories::all()->pluck('id'));

我看到這個(gè)錯(cuò)誤:

PDOException::(“SQLSTATE[HY000]:一般錯(cuò)誤:1364 欄位「id」沒有預(yù)設(shè)值」)

CategoryProductCategory 都使用 Uuidd 特徵,我不知道如何使其工作。

感謝任何幫助。

P粉714780768
P粉714780768

全部回覆(1)
P粉731977554

作為可能的解決方案之一,您可以使用自己的模型和資料透視表的特徵。

更多: https://laravel.com/docs/ 9.x/eloquent-relationships#defining-custom-intermediate-table-models。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板