Home > Mobile >  livewire does not update value
livewire does not update value

Time:06-02

i starting to use livewire and I know practically nothing.

the problem is that it doesn't update my counter without reloading the page:

Counter Screen

when I enter a new request remains the same value without updating

   <?php

namespace App\Http\Livewire;
use Illuminate\Http\Request;
use Livewire\Component;
use App\Models\Rrppsolicitudes;
use App\Models\Dias;
use Auth;

class Counter extends Component
{




    public function render(Request $request)
    {

       $evento = request()->segments()[1];
        $rrpp = Auth::user()->id;
        $counter = Rrppsolicitudes::where('evento_id' , $evento)->where('rrpp_id', $rrpp)->whereNull('estado')->count('id');
    
        return view('livewire.counter',compact('counter'));
    }
}

blade:

    <div style="background-color: #ff4040;
width: 117px;
font-size: 16px;
color: white;
border-radius: 15px;" >{{$counter}} Solicitudes</div>

what is the problem?

CodePudding user response:

If $counter is being updated by something outside of your component, the component won't know to refresh.

You can add wire:poll to your component or a part of it, which will refresh its values regularly.

Alternatively, you could have whatever's updating your $counter broadcast that change, and have Livewire listen for the broadcast using Laravel Echo. More complicated, but easier on the server and more immediate updates.

CodePudding user response:

Maybe you should have a look at Livewire - Computed Properties

  <?php

namespace App\Http\Livewire;
use Illuminate\Http\Request;
use Livewire\Component;
use App\Models\Rrppsolicitudes;
use App\Models\Dias;
use Auth;

class Counter extends Component
{
    public function getCounterProperty()
    {
        return Rrppsolicitudes::where('evento_id' , $evento)
            ->where('rrpp_id', $rrpp)
            ->whereNull('estado')
            ->count('id');
    }

    public function render(Request $request)
    {

       $evento = request()->segments()[1];
        $rrpp = Auth::user()->id;
    
        return view('livewire.counter');
    }
}
  • Related