AshwiniP's avatar

how to display all values stored in session.

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
7 replies
sujancse's avatar

Just dd(session()->all()); it will display all values stored in session

3 likes
AshwiniP's avatar

@st8113 tq but i want store all db values in session how can achieve this thing

sujancse's avatar

It's easy man just try parsing your session have a close look my result for

$session = session()->all(); is

array:3 [▼
  "_token" => "mmgqMBJYs1atx3KB4Mwm0mKksFpVbCQvSixMnRe6"
  "_previous" => array:1 [▶]
  "_flash" => array:2 [▶]
]

I am trying to access the _token

dd($session['_token']);

This will return "mmgqMBJYs1atx3KB4Mwm0mKksFpVbCQvSixMnRe6"

1 like
AshwiniP's avatar

@st8113 tnk you for ur reply i got my output. but its showing single value store in session. here is my store function. how can store all db values 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"; } }

sujancse's avatar

I don't know why are you trying to store all data in session but a simple foreach loop will do the trick for you. Just think about it.

Please or to participate in this conversation.