更新布尔值失败,因为在 null 上调用成员函数 update()

Updating boolean value fails because Call to a member function update() on null

我试图在用户按下按钮时将布尔字段从 false 更改为 true,但是当我执行代码时出现此错误:

 Call to a member function update() on null

这是我的迁移:

 public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('abteilung_name')->nullable()
            $table->boolean('isActive')->default(false);
            

        });
    }

我把它缩短了,我说的专栏是 isActive

这是我的控制器代码:

public function activate(Post $post, Request $request) {

        $postID = $request->id;

        $post = Post::find($postID);

        $post->update(['isActive' => 1]);


        return redirect()->back()->with('message', 'Anzeige veröffentlicht');
    }

这是用户按钮的 blade 代码:

 <form method="post" action="{{ route('activate', $post->id) }}">
                                    @csrf
                                <button type="submit" class="text-white px-4 py-3 rounded text-base font-medium
                                bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition
                                duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">Anzeige genehmigen
                                </button>
                                </form>

在数据库中,isActive 当前保存为“0”(零),我假设这是 laravel false 的方式,因为它默认为 false。我也试过这个:

$post->update(['isActive' => true]);

但是也没用。

错误是我在 Post $posts 的方法中已经有了 post 模型,所以我可以简单地这样做:

$post->update(['isActive' => 1]);

这样我的代码就更少了,我不需要搜索具有特定 ID 的 post,因为它已经通过方法中的参数识别