Laravel Livewire 模型 属性 绑定
Laravel Livewire model property binding
这是我的 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;
}
}
在我的 blade 视图中,我有
<button wire:click="setPriority({{ $postSingle->id }}, 4)"></button>
在其他地方我用 {{ $postSingle->priority }}
显示优先级
之所以不做模型属性直接绑定wire:model="postSingle.priority"
是因为我想运行$this->authorize('update', $postSingle);
.
下面的代码发生的情况是,如果我单击该按钮,blade 视图中的 $postSingle->priority
会更新,但 Post 记录不会在我的数据库中更新。我错过了什么?
您似乎忽略了实际保存记录。
public function setPriority(Post $postSingle, $priority)
{
$this->authorize('update', $postSingle);
$this->postSingle->priority = $priority;
// save the record
$this->postSingle->save();
}
这是我的 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;
}
}
在我的 blade 视图中,我有
<button wire:click="setPriority({{ $postSingle->id }}, 4)"></button>
在其他地方我用 {{ $postSingle->priority }}
之所以不做模型属性直接绑定wire:model="postSingle.priority"
是因为我想运行$this->authorize('update', $postSingle);
.
下面的代码发生的情况是,如果我单击该按钮,blade 视图中的 $postSingle->priority
会更新,但 Post 记录不会在我的数据库中更新。我错过了什么?
您似乎忽略了实际保存记录。
public function setPriority(Post $postSingle, $priority)
{
$this->authorize('update', $postSingle);
$this->postSingle->priority = $priority;
// save the record
$this->postSingle->save();
}