Can't set session manually - laravel

I'm new in Laravel 5.8 and I need to set a session manually I use this code Session::set('subset', 'yes'); but I get error how to set session manually in Laravel 5.8

Set the session variable with value as follows..
Session::put('key','value');
Retrieve value using
In controller
$value = Session::get('key');
In view
{{Session::get('key')}}

You can use the global session helper which is available in laravel 5.8.
Set session simply by using
session(['key' => 'value']);
Retrieve session using
$value = session('key');

Related

laravel get session data and order by latest

I'm working on a session data with Laravel and I can store sessions into a variable and retrieving the whole data. But my problem is I want to order the session to the latest 5 id's from the session stored.
Storing session
session()->push('products.recently_viewed', $product->getKey());
Retrieving session
$ids = session()->get('products.recently_viewed');
$recently_viewed = Product::whereIn('id', $ids)->get();
The retrieving is working fine, I only want to get the latest 5 id's from the session products.recently_viewed
e.g.
data from sessions
[1,3,7,6,8,9,11,2]
data I want to get
[2,11,9,8,6]
You need array_slice and array_reverse
$items = array_reverse(array_slice($yourSessionArray, -5));
-5 means "start at five elements before the end of the array".
the reverse will reverse the order of the array so the last is first.

How to get the session data into Codeigniter controller after user logged in?

I want to access session data after the user has logged in. I'm able to access session data in view pages but I want to access in the controller as well to create some database tables with session data like user id, name etc.
You can get session data:
$session = $this->session->userdata('session_name'); //Replace 'session_name' to your session name
print_r($session); //For check
You can details from Click Here
first set session variable in login controller as-
$this->session->set_userdata('userid', $row->iduser);
and use that session variable as-
$this->session->userdata('userid');
Following line will show all session data.
$session_data = $this->session->userdata();
echo "<pre>"; print_r($session_data); exit;

Delete session in laravel

I am using a session_variable as the user_id to get information about the user by querying with the help of the variable. Whenever my user logs in with correct credentials, I create a session variable using:
\Session::put('user_id', $user_id);
And now I have a condition when the user logs out from the website,so how do I remove the session variable from the session using laravel. I tried something like : session()->forget('some_data'); which didn't seem to work for me so what should I do to remove all the session variable I have in the webpage?
You can try with this
Session::flush(); // removes all session data
Session::forget('user_id') // Removes a specific variable

Laravel get values for Authenticated User only

Does laravel provide a more efficient way to only get values for the authenticated user instead of repeating the following line for every model type.
$cards = Auth::user()->cards;
$books = Auth::user()->books;
$friends = Auth::user()->friends;
.........
I can see how this can be accomplished by a middleware but wanted to see if laravel provides anything out of the box.
You can eager load relationships using eloquent's "with" method.

codeigniter session save to database keep updating many times

I have a question. I'm using latest version of CI.
I have currently enabled session to use the database, but there is only one problem with how it works since it tends to update the database every time you update your session.
Is there a way to fix that so that session would only update the database once before it destructs?!
I currently have around 9 update requests from one CI session only :) ! so any help would be lovely.
a simple Code example, my proccess is much more complicated than this, but just for demonstration purpose
Controller/auth [include post with true login]
function login(){
$trial=$this->user->attempt($this->input->post('user'),$this->input->post('pass'));
if($trial){
//1st update query
$this->session->set_userdata('user',$trial);
//Start intializing user
//2nd update query
$this->load->model('relations');//constructer will load user friends and save them to $this->session->set_userdata('relations',$rel);
//3rd update query
$this->load->model('settings');//construtor will load user settings and save them to settings
}
}
Above code will create 3 update request all targeted to update field userdata = current session.
my point is there is no need to update db every time we add something to session, its already saved in $_SESSION, so keep it there , and keep manipulating $_SESSION till end,
only updating user_data once at the end of all runtime -destructor- with the end result session is enough to save session to db.
please note that CI loads session from session using select * from ci_session (first request in picture) and then it just keep updating session repeatedly and pointless since it will not select it again during runtime !
so why not just LOAD it once session library is loaded. and save it just before it destruct. thats what im trying to accomplish.
Thanks
I have found a solution for this, get the existing user data from the session by using following code
$userdata = $this->session->all_userdata();
$userdata['fname'] = 'John';
$userdata['lname'] = 'Mulharn';
$userdata['email'] = 'Mulharn#john.com';
$this->session->set_userdata($userdata);
All this happens in 1 transaction instead of 3 database transaction.
Hope that helps.
A session may be updated by passing an array with all of the values, rather than doing them one at a time. Have another look in the Users' Guide

Resources