2

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?

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

As stated here you can use both request instance or session helper (your case), so this functionality is documented correctly (no shaming intended, just posting docs reference if somebody needed it)
Still the syntax is different and also Livewire 2.0 specific and you're responding to an year old comment.
Before posting i 've checked previous laravel docs, and v9 docs have the same explanation (that cover the year old question i suppose), said that i've bene landed on this question because i too was in need to store data in session in a Livewire 2 project, since the request session is provided from the Laravel scaffold fits to me that its functionality Is explained under laravel docs, not Livewire ones. I've found useful having some reference during development, that why i've added the link in the comment, i thought your correct answer (and its future viewers) will benefit from docs references
1

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>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.