简体   繁体   中英

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

when we make simple CRUD application. In our live-wire controller in edit method

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

    }

tables fields are '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.

Define a user property on your Livewire component, then wire the fields in your blade component to the properties of your user . You can then just call save on the user object.

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 :

Note: For this to work, you have a validation entry in the $rules property for any model attributes you want to bind to. Otherwise, an error will be thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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