Skip to content

Laravel Session() & Requests

Grace Burton edited this page Jun 8, 2020 · 1 revision

Sessions provide a way to store information about the user across multiple requests. The session config is stored at 'config/session.php'.

Using the session

Retrieving Data

2 ways of working with session data in Laravel: global session helper (session()) & via a Request instance. Both methods are testable using assertSessionHas(), available in all of the test cases.

Request instance

(Requires Illuminate\Http\Request class on controler method) Can be used as a parameter in controller functions or as a parameter with route closures.

Retrieving Input

  • Retrieve all data: $request->all();

  • Retrieving an input value: input() method can be used to retrieve user input. Passing input() without any arguments retrieves all the values in an associative array.

ex) $request->input('variable');

You can pass a default value as a second argument. When working with forms (which would return JSON arrays) use "dot" notation to access the arrays.

ex) $request->input('array.0.variable');

  • Retrieve input from query string: query() only retrieves values from query string. If the first value is not available, you can pass a second argument to return. query() will return all of the query string values as an associative array.

ex) $request->query('variable');

  • Retrieve Input via dynamic properties: If a form contains a field such as 'name', you can access it like so. Laravel will look for variable in the request payload, if not present, it will search for the field in the route parameters.

ex) $request->name;

  • Retrieve JSON input values: Access JSON data via input as long as Content-Type header of the request is set to 'application/json'. You can use dot notation to into the arrays. (**Is this useful to us?)

ex) $request->input('array.variable');

  • Retrieve Boolean input values: boolean() method retrieves value in true/false

ex) $request->boolean('variable');

  • Retrieving a portion of the input data: If you need only a subset of input data, use only() and except() methods. Both accept a single array or a dynamic list of arguments:

ex) $request->only(['username', 'password']); or without the array as a dynamic list ex) $input = $request->except(['credit_card']); or without the array as a dynamic list

Session data

  • Retrieving item from a session: When you retrieve an item from the session, you may also pass a default value as the second argument to the get method. This default value will be returned if the specified key does not exist in the session. If you pass a Closure as the default value to the get method and the requested key does not exist, the Closure will be executed and its result returned:

ex) $value = $request->session()->get('key', 'default');

ex) $value = $request->session()->get('key', function () { return 'default'; });

Global Session Helper

  • When session() is called with a single string argument, it will return the value of that session key, it will return the value of that session key. When it is called with an array of key & value pairs, those values will be stored in the session.

ex) Route::get('home', function () { // Retrieve a piece of data from the session... $value = session('key');

// Specifying a default value...
$value = session('key', 'default');

// Store a piece of data in the session...
session(['key' => 'value']);

});

Retrieving all session data:

ex) $request->session()->all();

Determining if an item exists in the session: Use has() method (returns true if item is present & NOT NULL)

ex) if ($request->session()->has('users')) { // }

To determine if an item is present in the session, even if its value is null, you may use the exists method. The exists method returns true if the item is present:

if ($request->session()->exists('users')) { // }

Clone this wiki locally