简体   繁体   中英

Get Session data in store function

Is it right to get Session data in a store function and store them into db?

public function store(){    
  ...
  $idgroup = Session::get('invitation_userid')];
  ...
}

Or need a store function always a Request Object?

public function store(Request $request){    
  ...
  $idgroup = $request('idgroup');
  ...
}

In both functions is of course a validation part for the input data.

Both approaches are fine, but you should use them appropriately to your use case, I prefer to use the Request data. The main difference is that if u store that inside the Session it will be available application wide, while if u send inside Request it will be available inside the method only

This depends entirely on the context of what your controller is actually named, how this data is being used and why you are not using a database session driver in the first place if you want to do this.

You could simply use the database driver for the session:

https://laravel.com/docs/5.7/session#introduction

It also depends on what your controller is named if you strictly want to follow restful routes:

https://gist.github.com/alexpchin/09939db6f81d654af06b

To answer the second question you don't always need a Request object in your store action. Most of the time you won't even see a Request object because you are simply creating an entirely new resource.

The Global Session Helper You may also use the global session PHP function to retrieve and store data in the session. When the session helper is called with a single, string argument, it will return the value of that session key. When the helper is called with an array of key / value pairs, those values will be stored in the session:

$value = session('key');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM