AlexioVay
AlexioVay

Reputation: 4527

Save data to sessions long term in Laravel Livewire?

I have a social network and want to store user interests likes/dislikes in a session since there are many subpages where I need this data and I want to avoid querying the database for that on every page. However, it seems you can only save flash data in sessions in Livewire.

The usual Laravel $request->session()->put(...) feature to save data long term in sessions doesn't seem to work in Livewire? So how could I save data long term in sessions or is there an other option or feature?

Upvotes: 2

Views: 5746

Answers (2)

Ivan
Ivan

Reputation: 29

In livewire component:

    <?php
    
    namespace App\Http\Livewire;
    
    use Livewire\Component;
    use App\Models\Producto; 
    use Livewire\WithPagination;
    
     class Filtro extends Component
    
    {
    
         public $marca;
    public function render()
    {


    if(isset($this->marca)){ session()->put('marca', $this->marca);}


            $productos = Producto::where('deleted',0)
                                ->where('marca','like', '%'.session('marca').'%')
                                
                                ->orderBy('id')
                                ->paginate(20) ;


        return view('livewire.filtro', [
            'productos' => $productos
        ]);
    }
}

In view blade:

<div class="filter__location">
    <input type="text"  class="form-control" placeholder="Marca" wire:model.debounce.1000ms="marca" 
    @if (session('marca'))

    value="{{ session('marca') }}"

     @endif

    />
        
    </div>

Upvotes: 1

AlexioVay
AlexioVay

Reputation: 4527

So, I just found out the answer by myself.

You can actually just use session()->put('key', 'value').

I wonder why this isn't officially documented tho. Maybe this helps someone of you too.

Upvotes: 10

Related Questions