简体   繁体   English

Laravel 8 & laravel-livewire 简单的 CRUD,带有值数组而不是单个值

[英]Laravel 8 & laravel-livewire simple CRUD with array of value instead of single value

when we make simple CRUD application.当我们制作简单的 CRUD 应用程序时。 In our live-wire controller in edit method在我们的火线 controller 编辑方法中

public function rEdit()
    {
        $this->data = auth()->user()->product()->where('id',$this->pid)->first()

    }

tables fields are 'id,name,price,....'表字段是 'id,name,price,....'

and then in live-wire blade we can get this value as然后在火线刀片中,我们可以得到这个值

<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="data.name" />

this is very simple example.这是一个非常简单的例子。

now this will not name value until I put name value in validation rule现在这不会命名值,直到我将名称值放入验证规则中

public function rules()
    {
        return [
            'sdata.name' => 'required',
        ];
    }

if I add name in rules it will show value in blade and if I remove it it will not show value.如果我在规则中添加名称,它将在刀片中显示值,如果我删除它,它将不会显示值。 I have 100s of field in table and some are required and some are not.我在表中有 100 个字段,有些是必需的,有些不是。

Define a user property on your Livewire component, then wire the fields in your blade component to the properties of your user .在 Livewire 组件上定义user属性,然后将刀片组件中的字段连接到user的属性。 You can then just call save on the user object.然后,您可以在user object 上调用save

class UserComponent extends Component
{
  public $user;

  public function mount()
  {
    $this->user = auth()->user();
  }

  public function save()
  {
    $this->user()->save();
  }
}
<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="user.name" />

<x-jet-input id="email" type="email" class="mt-1 block w-full" wire:model.defer="user.email" />

You will need to define validation rules for each of the properties on your model that you want to bind tosee the docs :您需要为要绑定的 model 上的每个属性定义验证规则以查看文档

Note: For this to work, you have a validation entry in the $rules property for any model attributes you want to bind to.注意:为此,您需要在$rules属性中为要绑定的任何 model 属性设置一个验证条目。 Otherwise, an error will be thrown.否则会抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM