Similar livewire components not working the same

What seems to be the problem: I’ve got two components that should function both the same but one function is letting me edit and the other one is giving me the following error: Typed property App\Http\Livewire\Contacts::$contact must not be accessed before initialization

Are you using the latest version of Livewire: 2.3.16

Do you have any screenshots or code examples:

This is the component that works:

<?php

namespace App\Http\Livewire;

use App\User;

use Livewire\Component;

use App\Mail\AddNewAdmin;

use App\Traits\HasFilters;

use Livewire\WithPagination;

use Illuminate\Validation\Rule;

use Spatie\Permission\Models\Role;

use Illuminate\Support\Facades\URL;

use Illuminate\Support\Facades\Mail;

class Admins extends Component

{

    use WithPagination, HasFilters;

    public $showModal = false;

    public $isDisabled = true;

    public $confirmingAdminDeletion = false;

    public $action = 'create';

    public User $admin;

    public $confirmEmail;

    public $role;

    public function rules()

    {

        $rules = [

            'admin.name' => 'required',

            'role' => 'required'

        ];

        $rules['admin.email'] = $this->action == 'create'

            ? 'required|email|unique:users,email|same:confirmEmail'

            : ['required', 'email', Rule::unique('users', 'email')->ignore($this->admin->id)];

        

        return $rules;

    }

    public function mount()

    {

        $this->admin = $this->makeEmptyUser();

    }

    public function create()

    {

        $this->fill([

            'showModal' => true, 

            'action' => 'create',

            'admin' => $this->makeEmptyUser()

        ]);

    }

    public function invite()

    {

        $data = $this->validate();

        $params = array_merge($data['admin'], ['role' => $this->role]);

        Mail::to($this->admin->email)->send(new AddNewAdmin(

            URL::temporarySignedRoute('register', now()->addDays(2), $params),

            $this->admin->name

        ));

        $this->showModal = false;

    }

    public function edit(User $admin)

    {

        $this->fill([

            'showModal' => true, 

            'action' => 'edit',

            'admin' => $admin,

            'role' => $admin->roles[0]->name

        ]);

    }

    public function update()

    {

        $this->validate();

        $this->admin->save();

        $this->admin->syncRoles($this->role);

        $this->clear();

    }

    public function confirmDeletion(User $admin)

    {

        $this->confirmingAdminDeletion = true;

        

        $this->admin = $admin;

    }

    public function delete()

    {

        $this->admin->delete();

        $this->confirmingAdminDeletion = false;

    }

    public function makeEmptyUser()

    {

        return User::make();

    }

    public function clear()

    {

        $this->reset([

            'showModal',

            'isDisabled',

            'action',

            'confirmEmail',

            'role'

        ]);

        $this->admin = $this->makeEmptyUser();

    }

    public function updatedShowModal()

    {

        $this->resetValidation();

    }

    public function updated()

    {

        $this->isDisabled = !($this->admin->name && $this->admin->email && $this->role);

    }

    public function render()

    {

        return view('livewire.admins', [

            'admins' => User::role(['super-admin', 'admin'])

                            ->where('name', 'LIKE', '%' . $this->search . '%')                

                            ->paginate($this->amount),

                            

            // 'roles'  => Role::all()

        ]);

    }

}

And this is the component that’s giving me the error:

<?php

namespace App\Http\Livewire;

use Livewire\Component;

use Livewire\WithPagination;

use Illuminate\Validation\Rule;

use Illuminate\Support\Facades\URL;

use Illuminate\Support\Facades\Mail;

use App\Mail\AddNewUser;

use App\Traits\HasFilters;

use App\User;

use App\Company;

use App\CompanyGroup;

class Contacts extends Component

{

    use WithPagination, HasFilters;

    public $showModal = false;

    public $confirmingContactDeletion = false;

    public $isDisabled = true;

    public $action = 'create';

    public $sites;

    public Company $company;

    public User $contact;

    public $confirmEmail;

    public $selectedSites = [];

    public function rules()

    {

        $rules = [

            'contact.name' => 'required',

            'contact.job_title' => 'nullable',

            'contact.phone' => 'nullable|numeric'

        ];

        $rules['contact.email'] = $this->action == 'create'

            ? 'required|email|unique:users,email|same:confirmEmail'

            : ['required', 'email', Rule::unique('users', 'email')->ignore($this->contact->id)];

        

        return $rules;

    }

    public function mount()

    {

        $this->contact = $this->makeEmptyContact();

        $this->selectedSites[] = $this->company->id;

        $this->sites = $this->company->group ? $this->company->group->sites : null;

    }

    public function create()

    {

        $this->showModal = true;

        $this->action = 'create';

    }

    public function invite()

    {

        $data = $this->validate();

        

        Mail::to($this->contact->email)->send(new AddNewUser(

            $this->generateSignedUrl($data['contact']), 

            $this->contact->name, 

            $this->company->company_name

        ));

        

        $this->showModal = false;

    }

    public function edit(User $contact)

    {

        $this->fill([

            'showModal' => true,

            'action' => 'edit',

            'contact' => $contact

        ]);

    }

    public function update()

    {

        $this->validate();

        $this->contact->save();

        $this->clear();

    }

    public function generateSignedUrl($params)

    {

        return URL::temporarySignedRoute(

            'register', now()->addHours(48), array_merge(

                ['company_ids' => implode(',', $this->selectedSites)], 

                $params

            )

        );

    }

    public function makeEmptyContact()

    {

        return User::make();

    }

    public function clear()

    {

        $this->reset(['showModal', 'isDisabled', 'action']);

        $this->contact = $this->makeEmptyContact();

    }

    public function updated()

    {

        $this->isDisabled = !( $this->contact->name && $this->contact->email && $this->confirmEmail);

    }

    public function render()

    {

        return view('livewire.contacts', [

            'contacts' => $this->company

                            ->contacts()

                            ->paginate($this->amount),

        ]);

    }

}

Both have an edit method that binds a model (admin and contact) but it seems like the validation is the thing causing the issue. But the admin one allows me to update whilst the contact one gives me the error.

Thank you.

I managed to solve this. Seemed to be an issue with where another property was being created.

Thanks :slight_smile: