BadMethodCallException (坏方法调用异常) |方法 Illuminate\Database\Schema\Blueprint::whereRaw 不存在

BadMethodCallException | Method Illuminate\Database\Schema\Blueprint::whereRaw does not exist

提问人:Aismaili 提问时间:5/1/2023 最后编辑:RiggsFollyAismaili 更新时间:5/1/2023 访问量:36

问:

在 Larevel 中,我无法迁移此表“收藏夹”,我遇到了这个错误

(方法 Illuminate\Database\Schema\Blueprint::whereRaw 不存在。

我试图使用“orWhere”和“where”来代替,但它是无用的,导致同样的错误......这是我的代码。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFavoritesTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('favorites', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->enum('item_type', ['question', 'answer']);
            $table->unsignedBigInteger('item_id');
            $table->timestamps();

            $table->unique(['user_id', 'item_type', 'item_id']);

            $connection = Schema::getConnection()->getDriverName();

            if ($connection === 'mysql') {
                $table->foreign('item_id')
                    ->references('id')
                    ->on('questions')
                    ->onUpdate('cascade')
                    ->onDelete('cascade');
                $table->whereRaw('(item_type = "answer" AND EXISTS (SELECT * FROM answers WHERE id = favorites.item_id))');
            } else {
                $table->morphTo('item');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::dropIfExists('favorites');
    }
}
php mysql laravel 错误处理 迁移

评论

0赞 aynber 5/1/2023
我不认为你可以有一个关于期权的条款whereON DELETE
0赞 lagbox 5/1/2023
@aynber似乎(代码方面)他们正试图将其直接应用于模式构建器,甚至不是一个流畅的字段
0赞 Tim Lewis 5/1/2023
你想在这里做什么? 在迁移中没有任何意义......此外,它是一个蓝图类,但您尝试使用的方法属于 BuilderModel 类......$table->whereRaw(...)$table->morphTo('item')$table
1赞 aynber 5/1/2023
@lagbox 是的,但这似乎是一个 X/Y 问题。 子句是 QueryBuilder 的一部分,而不是 SchemaBuilder 的一部分,但它们也尝试将子句应用于不支持它的表架构。似乎这种类型的删除级联应该在模型或模型事件上,而不是在架构中。wherewhere

答: 暂无答案