This is how you need to get session data in controller-
$request->session()->get('first_name');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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."; } }
@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.