Laravel灯塔嵌套突变不起作用



我正在使用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!]
}

有人知道问题出在哪里吗?

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

use IlluminateDatabaseEloquentRelationsBelongsTo;
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);        
}
}

最新更新