Search code examples
phplaravellaravel-livewire

Livewire 3 how to automatically assign data to public property?


I wanted to ask if there is a better way to create an update form using livewire 3? My current code doesn't seem effective if it has lots of properties in one form

public function openModalAkuisisi($uuid)
{
    $this->reloadData();

    $this->outlet = Outlet::akuisisi()->with(['regional', 'area', 'horecaGroup', 'horecaOutlet', 'statusTracking'])->whereUuid($uuid)->firstOrFail();

    $this->tp_code = $this->outlet->tp_code;
    $this->outlet_code = $this->outlet->outlet_code;
    $this->outlet_name = $this->outlet->outlet_name;
    $this->alamat = $this->outlet->alamat;
    ....
    ....
    ....
    ....

    $this->dispatch('open_modal');
}

Is there a better way to assign values to public properties with query results from Eloquent directly without writing them one by one? Maybe extract this validation rules data using https://livewire.laravel.com/docs/forms ?

$this->outlet = Outlet::akuisisi()->with(['regional', 'area', 'horecaGroup', 'horecaOutlet', 'statusTracking'])->whereUuid($uuid)->firstOrFail();
$this->form = $this->outlet;

So...my model inside form can bind with this data. Is that possible todo?


Solution

  • You can use the Bulk Assignment provided by Livewire.

    In your case, it would look something like this:

    public function openModalAkuisisi(string $uuid): void
    {
        $this->reloadData();
    
        $this->outlet = Outlet::akuisisi()
            ->with(['regional', 'area', 'horecaGroup', 'horecaOutlet', 'statusTracking'])
            ->whereUuid($uuid)
            ->firstOrFail();
    
        $this->fill($this->outlet->only([
            'tp_code',
            'outlet_code',
            'outlet_name',
            'alamat',
            // ... Add other properties
        ]));
    
        $this->dispatch('open_modal');
    }
    

    If you want to put it into a Livewire Form, you could do the same:

    namespace App\Livewire\Forms;
    
    use App\Models\Outlet;
    use Livewire\Form;
    
    final class OutletForm extends Form
    {
        public Outlet $outlet;
    
        public string $tp_code;
        public string $outlet_code;
        public string $outlet_name;
        public string $alamat;
        // ... Define all the properties.
    
        public function set(Outlet $outlet): void
        {
            $this->outlet = $outlet;
    
            $this->fill($outlet->only([
                'tp_code',
                'outlet_code',
                'outlet_name',
                'alamat',
                // ... Add other properties
            ]));
        }
    }
    

    And your Livewire component would look something like this:

    namespace App\Livewire;
    
    use App\Livewire\Forms\OutletForm;
    use App\Models\Outlet;
    use Livewire\Component;
    
    final class EditarOutlet extends Component
    {
        public OutletForm $form;
    
        public function openModalAkuisisi(string $uuid): void
        {
            $this->reloadData();
    
            $this->outlet = Outlet::akuisisi()
                ->with(['regional', 'area', 'horecaGroup', 'horecaOutlet', 'statusTracking'])
                ->whereUuid($uuid)
                ->firstOrFail();
    
            $this->form->set($outlet);
    
            $this->dispatch('open_modal');
        }
    
        // Other methods of your component
    }