0

我正在使用 Laravel Lighthouse 为我的新应用程序创建 GraphQL API,现在 Nested Belongs to and Belongs to Many 不适用于创建和更新方法,在数据透视表中,没有添加任何内容,我收到以下错误:

"SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into `products` (`title`, `updated_at`, `created_at`) values (new product, 2020-05-03 19:40:59, 2020-05-03 19:40:59))"

产品迁移:

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->unsignedBigInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->integer('status')->default(1);
            $table->softDeletes();
            $table->timestamps();
        });
    }

标签迁移:

public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->unsignedBigInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->integer('status')->default(1);
            $table->softDeletes();
            $table->timestamps();
        });
    }

枢轴迁移:

public function up()
    {
        Schema::create('product_tag', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->unsignedBigInteger('tag_id');
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
            $table->timestamps();
        });
    }

产品型号:

class Product extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'title', 'category_id', 'status'
    ];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
}

标签型号:

class Tag extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'title', 'category_id', 'status'
    ];

    protected static function booted()
    {
        static::deleted(function ($tag) {
           $tag->products()->detach();
        });
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class);
    }
}

产品架构

extend type Query @guard(with: ["sanctum"]) {
    products(orderBy: _ @orderBy trashed: Trashed @trashed): [Product] @all
}

extend type Mutation @guard(with: ["sanctum"]) {
    createProduct(
        title: String! @rules(apply: ["required"])
        category: CreateCategoryRelation
        tags: UpdateProductTags
    ): Product @create

    updateProduct(
        id: ID!
        title: String! @rules(apply: ["required"])
        category: CreateCategoryRelation
        tags: UpdateProductTags
    ): Product @update

    deleteProduct(
        id: ID!
    ): Product @delete
}

type Product {
    id: ID!
    title: String!
    category: Category!
    tags: [Tag!]
}

input CreateCategoryRelation {
    connect: ID!
}

input UpdateProductTags {
    connect: [ID!]
    sync: [ID!]
}

有谁知道是什么问题?

4

1 回答 1

1

我找到了解决方案,Lighthouse 使用类型提示来确定它应该如何处理这种关系。

use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Post extends Model 
{
    // WORKS
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    // DOES NOT WORK
    public function comments()
    {
        return $this->hasMany(Comment::class);        
    }
}
于 2020-05-04T09:17:49.627 回答