【发布时间】:2020-07-25 05:16:46
【问题描述】:
我正在将 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 列。这是我的类实现:
<?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
【问题讨论】:
-
你能分享你要删除的代码吗?
-
我使用 tinker 来尝试删除,例如 App\Comment::first() - >delete()。它适用于其他型号。
-
试试这个
dd(Comment::first()) ;并将输出粘贴到这里。 -
这里在 tinker App\Comment {#3667 id: "01418755-c68e-4ea5-8043-cef348c47445", content: "Super frangins", 剧透:0,隐藏:0,编辑: 0, parent_id: null, user_id: "5311700e-6271-442d-84b6-f810d902001f", commentable_id: "d299b3d2-7996-46eb-a3e9-bd2883647aff", commentable_type: "App\Amadiora", meta: null, created_at: "2019 -03-21 06:43:22", updated_at: "2019-03-21 06:43:22", } ps:抱歉,我不知道如何在 cmets 中格式化代码
-
我将模型更改为扩展 Pivot 而不是 MorphPivot 并且可以毫无问题地删除它我认为 MorphPivot 删除方法可能是这里的问题
标签: php laravel eloquent polymorphic-associations