Hello, everybody, I need some help in my project. I am still using PHP laravel framework for my web development. I am new using Laravel 5.2.
I would like to get session value from session variable and select data from database where session variable is match. I already define a session variable in the login time, but I can get user data from MySQL table. I want to select particular one user data, so I need to use session in where clause. I don’t know how to use it? help me please.
Hi, thanks for asking your problem here. We will try to solve your problem. Follow our guideline it will help you.
First you need to store session value in a variable, which session value you are already set in the another pages after you work success. To get session variable use this code:
Session::get(‘variable_name’)
For display session value or check if session value is exist or not, apply this example code:
<?php if(!empty(Session::get('variable_name'))){ echo Session::get('variable_name'); }else{ echo "Not found!"; } ?>
To select data using session variable. Follow the code:
Controller:
If you need to display selected data, then send the selected data in the view page by Controller. Follow below code for your help.
<?php $data=with(new Model)->GetSessionData(); return view('dashboard')->with('data',$data); ?>
Use in Model:
Put your MySQL table name in the table name position. Now put session variable in the where clause or you can put session value directly without store in a variable. Here I also put session in the where clause directly in select query. return data after selected user result.Follow this example code for better understand.
$data=DB::table('table_name')->where('user_id',Session::get('user_id'))->get(); return $data;