Laravel - Get items from session array
When you're using push()
, an array will be created, so you need to do something like this:
{{ session('basic_settings.0.company_name') }}
Or:
{{ session('basic_settings')[0]['company_name'] }}
But I would recommend you to save the data with:
{{ session(['basic_settings' => request()->all()]) }}
Then you'll be able to read it with:
{{ session('basic_settings.company_name') }}
Related videos on Youtube
Rainier laan
Updated on June 04, 2022Comments
-
Rainier laan almost 2 years
I'm making a step program and therefore I wanted to use sessions. I have a form which allows the user to fill in some information and then the input values will be put in a session array called "basic_settins" like this
public function saveSettings(Request $request, Product $products) { $request->session()->push('basic_settings', $request->input()); return redirect('/themes'); }
But how can I get a specific item from that array and display it for instance in my view? By using something like this:
{{ Session::get('store_name') }}
in an input field.The view looks like this:
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-12"> <form method="POST" action="{{ route('saveBasicSettings') }}"> <div class="row"> <div class="col-md-12"> <button class="btn btn-primary mb-3 float-right">Next</button> </div> </div> <div class="card z-depth-2" style="border-radius: 7px;"> <div class="card-body"> <input type="hidden" name="user_id" value="{{ Auth::user()->id }}"> {{ csrf_field() }} <div class="form-group"> <label for="store_name">Store name</label> <input type="text" class="form-control" placeholder="Store name" id="store_name" name="store_name" value="{{ Session::get('store_name') }}" required> </div> <div class="form-group"> <label for="company name">Company name</label> <input type="text" class="form-control" placeholder="Company name" id="company_name" name="company_name" value="{{ Session::get('company_name') }}" required> </div> <div class="form-group"> <label for="company_tel">Company phonenumber</label> <input type="text" class="form-control" placeholder="Company Phonenumber" id="company_tel" name="company_tel" value="{{ Session::get('company_tel') }}" required> </div> </div> </div> </form> </div> </div> </div> @endsection
When I dd the request by doing this:
$data = $request->session()->all();
and thendd($data);
I get the following result:
How can I make this work? Thanks in advance!
-
vijaykumar about 6 yearsI guess you have to use like this Session::get('basic_settings.0.company_tel')
-
-
Rainier laan about 6 yearsI got it working thanks, Do you also know how I can retrieve a specific item in the controller? Instead of using a blade? Because then I know a way to insert the values into a DB table called basic_settings
-
Alexey Mezenin about 6 years@Rainierlaan
$companyName = session('basic_settings.0.company_name');