0

livewire component

<?php

namespace App\Http\Livewire;

use App\Models\Location;
use Livewire\Component;

class ManageLocation extends Component
{

    public $locations;
    public $unitNo;
    public $street;
    public $city;
    public $state;
    public $postCode;
    public $user;
    public $showAddForm = false;
    

    public function render(){
        $this->getUserLocations();
        return view('livewire.manage-location');
    }

    public function addLocation(){
        $locations= new Location();
        $locations->unitNo = $this->unitNo;
        $locations->street = $this->street;
        $locations->city = $this->city;
        $locations->state = $this->state;
        $locations->postCode = $this->postCode;

        //get logged-in user id as reference
        $locations->id=auth()->User()->id;

        $locations->save();
        $this->reset(['unitNo','street','city','state','postCode']);
    }

    public function getUserLocations(){
        $this->locations = Location::where('id', auth()->user()->id)->get();
        
    }
   

    //manage add location form method

    //1
    public function showForm(){
        $this->showAddForm = !$this->showAddForm;
        $this->resetForm();
    }
    //2
    public function resetForm(){
        $this->unitNo = '';
        $this->street = '';
        $this->city = '';
        $this->state = '';
        $this->postCode = '';
    }

    public function updateLocation()
    {
            $location = Location::find($this->locationID);
    
            if ($location) {
                $location->update([
                    'unitNo' => $this->unitNo,
                    'street' => $this->street,
                    'city' => $this->city,
                    'state' => $this->state,
                    'postCode' => $this->postCode
                ]);
            }
        
    }
    


  
}

blade view

@foreach($locations as $location)
<div>
    <form wire:submit.prevent="updateLocation('{{ $location->locationID }}')">
        <div>
            <label for="unitNo">Unit No</label>
            <input type="text" placeholder="{{ $location->unitNo }}" wire:model="unitNo">

            <label for="street">Street</label>
            <input type="text" placeholder="{{ $location->street }}" wire:model="street">

            <label for="city">City</label>
            <input type="text" placeholder="{{ $location->city }}" wire:model="city">

            <label for="state">State</label>
            <input type="text" placeholder="{{ $location->state }}" wire:model="state">

            <label for="postCode">Post Code</label>
            <input type="text" placeholder="{{ $location->postCode }}" wire:model="postCode">
        </div>
        <button type="submit" class="btn border-black">Update</button>
    </form>
</div>
@endforeach

HTML for better understandingCould you guys please help me troubleshoot my code, i am trying to make a form where it has placeholder of saved locations based on the userID and when i type in the placeholder and click the button, it could update into database based on the locationID of the location.

What should i do to make it work? Thank you.

1 Answers1

0

wire:submit.prevent="updateLocation('{{ $location->locationID }}')" here you're passing locationID but your function public function updateLocation() doesn't have $locationID for further control.

Change your function like this public function updateLocation($locationID)

Also change this $location = Location::find($this->locationID); to $location = Location::findOrFail($locationID);

Vpa
  • 703
  • 2
  • 9
  • 30