Home > OS >  How do I fetch a specific row from a db based on input, to add to another db? Laravel/Livewire
How do I fetch a specific row from a db based on input, to add to another db? Laravel/Livewire

Time:11-15

current code shows error "Unable to resolve dependency [Parameter #0 [ $identity ]] in class App\Http\Livewire\Users"

this code would hopefully grab the input ($identity), searches personnel table according to accountno or email then adds it to users table,

im using these functions

    public function personnelCheck($identity)
    {        
        $this->resetErrorBag();
        $user = Personnel::where('AccountNumber', $identity)
                ->orWhere('OfficialEmail', $identity)->first();

        $this->personnelExists = true;
    }
    
    public function saveUser() 
    {
        $pw = User::generatePassword();
       
        User::create([
            'accountno' => $user['AccountNumber'],
            'email' => $user['OfficialEmail'],
            'password' => $pw,
            'password_changed_at' => Carbon::now()->toDateTimeString(),
            'status' => 0
        ]);

        $this->confirmingUserAdd = false;
 
    }

my front end is

<x-dialog-modal wire:model="confirmingUserAdd">
                @if($personnelExists == false)
                    <x-slot name="title">
                        {{ __('Enter Personnel Detail to Add User') }} {{$identity}}
                    </x-slot>
            
                    <x-slot name="content">
                        <div >
                            <x-label for="identity" value="{{ __('Personnel Account Number or Official Email') }}" />
                            <x-input id="identity" type="text"  wire:model.defer="identity" />
                            <x-input-error for="identity"  />
                        </div>
                    </x-slot>

                    <x-slot name="footer">
                        <x-jet-secondary-button wire:click="$set('confirmingUserAdd', false)" wire:loading.attr="disabled">
                            {{ __('Cancel') }}
                        </x-jet-secondary-button>
            
                        <x-jet-danger-button  wire:click="personnelCheck({{$identity}})" wire:loading.attr="disabled">
                            {{ __('Add Personnel') }}
                        </x-jet-danger-button>
                    </x-slot>
                @else
                    <x-slot name="title">
                        {{ __('Personnel Details') }}
                    </x-slot>
            
                    <x-slot name="content">
                        <div >
                            @if($personnelExists == true)
                                Personnel: {{ $user->LastName }}, {{ $user->FirstName }}</br>
                                Gender: {{ $user->GenderDesc }}</br>
                                Official Email: {{ $user->OfficialEmail }}</br>
                            @endif
                        </div>
                    </x-slot>

                    <x-slot name="footer">
                        <x-jet-secondary-button wire:click="$set('confirmingUserAdd', false)" wire:loading.attr="disabled">
                            {{ __('Cancel') }}
                        </x-jet-secondary-button>
            
                        <x-jet-danger-button  wire:click="saveUser()" wire:loading.attr="disabled">
                            {{ __('Add Personnel') }}
                        </x-jet-danger-button>
                    </x-slot>
                @endif
            </x-dialog-modal>

button click on full page livewire shows this modal, wherein before entering $identity it will prompt that input field and after it will show details of the personnel.

i tried not passing the variable to the personnelCheck function,

    public function personnelCheck()
    {        
        $this->resetErrorBag();
        $user = Personnel::where('AccountNumber', $identity)
                ->orWhere('OfficialEmail', $identity)->first();

        $this->personnelExists = true;
    }
    

i figured it might not be needed to be declared since the form is already setting $identity variable but whatever i do with the personnelCheck, laravel tells me on the error page that $identity is set to null anw. i never got to set the $identity variable.

Thanks a lot!

CodePudding user response:

    public function personnelCheck()
    {        
        $this->resetErrorBag();
        $user = Personnel::where('AccountNumber', $this->identity)
                ->orWhere('OfficialEmail', $this->identity)->first();

        $this->personnelExists = true;
    }

$this->identity should be declared as class public variable

  • Related