Search code examples
laravellaravel-livewire

Laravel Livewire: after any method call, my array gets nullified


So i have a livewire component that has two properties a $viewedJob and $jobs which is a collection of jobs that are related to $viewedJob. my logic is that. When you click on a job, that job becomes $viewedJob easy task.

so i set up a livewire method to do the logic

public function viewJob($jobId) 
{
    $this->viewedJob = Job::find($jobId);
    $this->jobs = $this->jobs;
}

and inside my frontend i call it this way

<button wire:click="viewJob({{$job}})" class="truncate">
    {{$job->title}}
</button>

MY PROBLEM

the problem is that whenever the viewJob method gets called, the $jobs array gets nulliefied, each peoprty inside it will become null and i get this error

enter image description here

i tried also not doing anything inside viewJob and still get the same error

public function viewJob($jobId) 
{
    // although i dont do anything, i still get the same error!!
}

any ideas will help, thank you for your time.


Solution

  • if your frontend code is in a loop of $jobs variable you should try this.

    <button wire:click="viewJob({{$job->id}})" class="truncate">
        {{$job->title}}
    </button>
    

    if this not working you should make your $jobs collection into an array like:

    $this->jobs = Job::all()->toArray();
    

    and in frontend you can write like:

    <button wire:click="viewJob({{$job['id']}})" class="truncate">
        {{$job['title']}}
    </button>
    

    I hope it will work. My answer is on based that your front end code is inside a loop.