Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AshwiniP's avatar

values stored in session are not displaying in array

hello every one i am new to laravel. i am trying to store db values in session and want to display whenever its needed. but i cant display all values of db.but its displaying only one value. please anybody can help me. here is my controlller code namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\Http\Requests; use App\Customer; use App\Http\Controllers\Controller; use Session;

class SessionController extends Controller { public function accessSessionData(Request $req) { if($req->session()->has('first_name'))

    echo $val=Session::get('first_name');
  else
     echo 'No data in the session';

} public function storeSessionData(Request $request,$id)

{ $customers=Customer::where('first_name','LIKE','%'.$request->$id.'%')->get(); foreach($customers as $key =>$customer) { $request->session()->put('first_name',$customer->first_name); echo "Data has been added to session"; } } public function deleteSessionData(Request $request){ $request->session()->forget('first_name'); echo "Data has been removed from session."; } }

0 likes
13 replies
AshwiniP's avatar

@tisuchi tnk u for ur reply. i tried but only one value is displayed. but i wnat to display all values which are stored in db

bipin's avatar

try to use this

   $request->session()->all('');
AshwiniP's avatar

@bipin thank you for yuor reply now its displaying the message no data has been found

tisuchi's avatar

Follow @bipin comment by changing a bit-

$request->session()->all();

** No quote inside all('').

AshwiniP's avatar

@tisuchi i tried but its showing the following error message ErrorException in SessionController.php line 18: Array to string conversion

nagavinod424's avatar

just dump values like this

{{ dump(request()->sessions()->all()) }}

nagavinod424's avatar

use ``` at starting and ending of the code so that we can understand your code

tisuchi's avatar

Can you show your code how do you store value in session?

AshwiniP's avatar

public function storeSessionData(Request $request,$id)

{ $customers=Customer::where('first_name','LIKE','%'.$request->$id.'%')->get(); foreach($customers as $key =>$customer) { $request->session()->put('first_name',$customer->first_name); echo "Data has been added to session"; } }

AshwiniP's avatar

@tisuchi public function storeSessionData(Request $request,$id)

{ $customers=Customer::where('first_name','LIKE','%'.$request->$id.'%')->get(); foreach($customers as $key =>$customer) { $request->session()->put('first_name',$customer->first_name); echo "Data has been added to session"; } }

and hee is my route Route::get('session/get','SessionController@accessSessionData'); Route::get('session/set{id}','SessionController@storeSessionData');

sujancse's avatar

Just dd(session()->all()); it will return you an array with key value pairs. Then try accessing your data using your desired key.

AshwiniP's avatar
AshwiniP
OP
Best Answer
Level 1

@st8113 ya i used that code. but its showing the output like this. array:4 [▼ "_token" => "aXVTOUQxW1UKVqf0GisLJ3hpGwnKXBs6zAPj6MPg" "first_name" => "gt" "_previous" => array:1 [▼ "url" => "http://localhost:8000/session/get" ] "_flash" => array:2 [▼ "old" => [] "new" => [] ] ] what does it mean

Please or to participate in this conversation.