Livewire:如何获取新创建行的 ID?

Livewire: how to grab ID of newly created row?

我用 livewire 创建了一个按钮,当它被点击时。它将在数据库中生成一个新的看板。

<div>
    <form>
        <button wire:click="addKanban" class="h-8 w-8 rounded bg-blueGray-100  text-lg text-gray-900 focus:outline-none hover:text-white hover:bg-blue-500 ">
            <i class="bi bi-plus"></i>
        </button>
    </form>
</div>

livewire php file中这是函数

public function addKanban ()
    {
        $record = $this->validate([
            'team_id' => 'required',
            'icon' => 'required',
            'color' => 'required',
            'title' => 'required',
            'order' => 'required',
            'status' => 'required'
        ]);

        Kanban::create($record);

        $this->resetInputFields();
        
        return redirect()->route('kanbans.show-all',['kanban'=>$this->$kanban->id]);
    }

在数据库中创建行后,我希望它重定向到显示该看板的页面。我找到了有关如何操作的文档 https://laravel.com/docs/8.x/redirects,但我似乎无法获得新的看板 id

这应该是 return redirect()->route('kanbans.show-all',['kanban'=>'new kanban id']);

我得到的最接近的是 ['kanban'=>$this->id] 然而,这个 returns livewire 创建的随机字符串 ID,它转到 team/kanban/a69J5LmObUjcKbmJYPxE/show-all 而不是 team/kanban/1/show-all

非常感谢任何帮助或见解:)

public function addKanban ()
{
        $record = $this->validate([
            'team_id' => 'required',
            'icon' => 'required',
            'color' => 'required',
            'title' => 'required',
            'order' => 'required',
            'status' => 'required'
        ]);

        $modelCreated = Kanban::create($record);

        $this->resetInputFields();
        
        return redirect()->route('kanbans.show-all',['kanban'=>$modelCreated->id]);
    }

就这些,记得通过路由模型绑定的方式正确管理,希望对你有帮助