【问题标题】:Laravel Livewire model property bindingLaravel Livewire 模型属性绑定
【发布时间】:2021-09-10 00:07:33
【问题描述】:

这是我的 Livewire 组件

<?php

namespace App\Http\Livewire\Dashboard;

use App\Models\Post;
use Livewire\Component;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class EditPost extends Component
{
    use AuthorizesRequests;

    public ?Post $postSingle = null;

    protected $rules = [
        'postSingle.priority' => 'digits_between:1,5',
    ];

    public function setPriority(Post $postSingle, $priority)
    {
        $this->authorize('update', $postSingle);
        $this->postSingle->priority = $priority;
    }

}

在我的刀片视图中,我有 &lt;button wire:click="setPriority({{ $postSingle-&gt;id }}, 4)"&gt;&lt;/button&gt;

在其他地方我用{{ $postSingle-&gt;priority }}显示优先级

我不直接用wire:model="postSingle.priority"做模型属性绑定的原因是我想跑$this-&gt;authorize('update', $postSingle);

下面的代码发生的情况是,如果我单击按钮,刀片视图中的$postSingle-&gt;priority 会更新,但我的数据库中的 Post 记录不会更新。我错过了什么?

【问题讨论】:

  • 属性赋值后使用 save() 方法

标签: laravel laravel-livewire


【解决方案1】:

您似乎忽略了实际保存记录。

public function setPriority(Post $postSingle, $priority)
{
    $this->authorize('update', $postSingle);
    $this->postSingle->priority = $priority;

    // save the record
    $this->postSingle->save();
}

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 2021-07-09
    • 1970-01-01
    • 2021-10-13
    • 2021-04-17
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多