无法删除 morppivot 自定义模型 laravel
Cannot delete morphpivot Custom model laravel
我正在为一个项目使用 laravel,在删除自定义 morphPivot 关系时遇到了一些问题。
当我尝试删除关系时,出现以下错误:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: delete from `comment` where (`` = 01418755-c68e-4ea5-8043-cef348c47445))'
从中我认为 laravel 正试图通过 ID 删除,但 ID 列没有名称,因此无法找到它。但是,我想我清楚地定义了 id 列。这是我的 class 实现:
<?php
namespace App;
use App\Traits\CanBeBlocked;
use App\Traits\CanBeGhosted;
use App\Traits\CanBeVoted;
use App\Traits\HasUuid;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Support\Facades\DB;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
class Comment extends MorphPivot
{
use HasUuid,
CanBeBlocked,
CanBeGhosted,
CanBeVoted;
protected $casts = [
'id' => 'string'
];
protected $fillable = [
'id',
'parent_id',
'user_id',
'commentable_id',
'commentable_type',
'content',
'created_at',
'updated_at'
];
public $incrementing = false;
public $keyType = 'string';
protected $primaryKey = 'id';
/**
* @brief Users commenting another model
*/
public function commenter()
{
return $this->belongsTo('App\User', 'user_id');
}
/**
* @brief models commented by a User
*/
public function commentable()
{
return $this->morphTo();
}
/**
* @brief replies of the comment
*/
public function replies()
{
return $this->hasMany('App\Comment', 'parent_id');
}
public function parent()
{
return $this->belongsTo('App\Comment');
}
public function repliers()
{
return $this->replies->map(function ($reply, $key) {
return $reply->commenter;
});
}
public function isReply()
{
return $this->parent_id !== null;
}
public function commentOnly($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder
{
return Comment::where('parent_id', null);
}
}
和迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comment', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
$table->text('content');
$table->boolean('spoiler')->default(false);
$table->boolean('hidden')->default(false);
$table->boolean('edited')->default(false);
$table->uuid('parent_id')->nullable();
$table->uuid('user_id');
$table->uuid('commentable_id');
$table->string('commentable_type');
$table->json('meta')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comment');
}
}
预先感谢您的帮助。
Laravel v6.18.26
现已在 Laravel 最新版本中修复
我正在为一个项目使用 laravel,在删除自定义 morphPivot 关系时遇到了一些问题。
当我尝试删除关系时,出现以下错误:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: delete from `comment` where (`` = 01418755-c68e-4ea5-8043-cef348c47445))'
从中我认为 laravel 正试图通过 ID 删除,但 ID 列没有名称,因此无法找到它。但是,我想我清楚地定义了 id 列。这是我的 class 实现:
<?php
namespace App;
use App\Traits\CanBeBlocked;
use App\Traits\CanBeGhosted;
use App\Traits\CanBeVoted;
use App\Traits\HasUuid;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Support\Facades\DB;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
class Comment extends MorphPivot
{
use HasUuid,
CanBeBlocked,
CanBeGhosted,
CanBeVoted;
protected $casts = [
'id' => 'string'
];
protected $fillable = [
'id',
'parent_id',
'user_id',
'commentable_id',
'commentable_type',
'content',
'created_at',
'updated_at'
];
public $incrementing = false;
public $keyType = 'string';
protected $primaryKey = 'id';
/**
* @brief Users commenting another model
*/
public function commenter()
{
return $this->belongsTo('App\User', 'user_id');
}
/**
* @brief models commented by a User
*/
public function commentable()
{
return $this->morphTo();
}
/**
* @brief replies of the comment
*/
public function replies()
{
return $this->hasMany('App\Comment', 'parent_id');
}
public function parent()
{
return $this->belongsTo('App\Comment');
}
public function repliers()
{
return $this->replies->map(function ($reply, $key) {
return $reply->commenter;
});
}
public function isReply()
{
return $this->parent_id !== null;
}
public function commentOnly($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder
{
return Comment::where('parent_id', null);
}
}
和迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comment', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
$table->text('content');
$table->boolean('spoiler')->default(false);
$table->boolean('hidden')->default(false);
$table->boolean('edited')->default(false);
$table->uuid('parent_id')->nullable();
$table->uuid('user_id');
$table->uuid('commentable_id');
$table->string('commentable_type');
$table->json('meta')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comment');
}
}
预先感谢您的帮助。
Laravel v6.18.26
现已在 Laravel 最新版本中修复