vildanbina / livewire-tabs

Livewire component that provides you with a tabs that supports multiple tabs form while maintaining state.
MIT License
48 stars 10 forks source link

Model data not binding #3

Closed cstor01 closed 1 year ago

cstor01 commented 1 year ago

Hello, I followed the readme step by step to reproduce the example and edit the user table. Even if the model data is available on the general.blade.php the input fields are empty. My specification are:

Laravel v10.13.5 livewire v2.12.3

some code excerpts:

//  resources/views/tabs/general.blade.php
<input type="text" class="form-control @error('title') is-invalid @enderror" id="name" name="name" placeholder="Enter Name" wire:model="name">
<input type="text" class="form-control @error('title') is-invalid @enderror" id="email" name="email" placeholder="Enter Email" wire:model="email">
// routes/web.php
Route::get('/dashboard/users/{userId}/', \App\Http\Livewire\UserTab::class)
    ->name('dashboard.user.view');
// app/Http/Livewire/UserTab.php
<?php

namespace App\Http\Livewire;

use App\Models\User;
use App\Tabs\General;
use Vildanbina\LivewireTabs\TabsComponent;
use WireUi\Breadcrumbs\Trail;

class UserTab extends TabsComponent
{
    public function breadcrumbs(Trail $trail): Trail
    {
        return $trail
            ->push('Dashboard')
            ->push('Manage users', route('dashboard.users'));
    }
    // My custom class property
    public $userId;
    public $name;
    public $email;
    /*
     * Will return App\Models\User instance or will create empty User (based on $userId parameter)
     */
    public function model()
    {
        return User::findOrNew($this->userId);
    }

    public array $tabs = [
        General::class,
        // Other tabs...
    ];
}

It's also impossible to update the model, thanks in advance for any help.

vildanbina commented 1 year ago

Hi @cstor01,

In your blade templates, it appears that state.name and state.email should be used as wire:model instead of directly using public properties. The reason is that in this package, all the necessary data is stored in the $state variable, rather than individual public properties. So, your blade templates should look something like:

<div>
    <label for="name">Name</label>
    <input type="text" id="name" wire:model="state.name" />

    <label for="email">Email</label>
    <input type="text" id="email" wire:model="state.email" />
</div>

Let me know if this helps, or if you have any more questions.

cstor01 commented 1 year ago

Thanks so much, it works! I was stuck to this for a couple of days.

humayunjavaid commented 2 weeks ago

i have made location tab but on submit General tab save function calls? how to call Location tab save method to save location form fields?