{"id":9770,"date":"2021-04-02T10:00:56","date_gmt":"2021-04-02T01:00:56","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9770"},"modified":"2021-04-01T19:39:43","modified_gmt":"2021-04-01T10:39:43","slug":"laravel-session","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9770","title":{"rendered":"Laravel Session"},"content":{"rendered":"\n<p>Sessions provide a way to store information about the user across multiple requests. Session configuration file is stored at config\/session.php. Laravel provide various drivers like file, cookie, database, mechace \/ redis, dynamodb, array. By default, Laravel is configured to use <strong>file <\/strong>session driver. <\/p>\n\n\n\n<h4>Interaction with the Session<\/h4>\n\n\n\n<p><strong>Retrieving Data<\/strong><\/p>\n\n\n\n<p>There are two primary ways to access the session data in laravel: the global <strong>session <\/strong>helper and via a <strong>Request <\/strong>instance.&nbsp;<\/p>\n\n\n\n<p> To access the session data via HTTP Request, we can use the<strong> get()<\/strong> method, which will take one argument, key to get the session data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request->session()->get('key');<\/code><\/pre>\n\n\n\n<p>When we retrieve an item from the session, we may also pass a default value as the second argument to the get method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request->session()->get('key', 'default');<\/code><\/pre>\n\n\n\n<p>To retrieve all the data in the session, we may use the <strong>all <\/strong>method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$data = $request->session()->all();<\/code><\/pre>\n\n\n\n<p>To determine if an item is present in the session, we may use the <strong>has <\/strong>method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ($request->session()->has('users')) {\n    \/\/\n}<\/code><\/pre>\n\n\n\n<p><strong>Pushing To Array Session Value<\/strong><\/p>\n\n\n\n<p>The <strong>push <\/strong>method may use to push a new value onto a session value that is an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request->session()->push('user.teams', 'developers');<\/code><\/pre>\n\n\n\n<p><strong>Retrieving &amp; Deleting An Item<\/strong><\/p>\n\n\n\n<p>The <strong>pull <\/strong>method may used to retrieve and delete an item form the session in a single statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$value = $request->session()->pull('key', 'default');<\/code><\/pre>\n\n\n\n<p>If session data contains an integer we may wish to increment or decrement, we may use the <strong>increment <\/strong>and <strong>decrement <\/strong>methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request->session()->increment('count');\n$request->session()->decrement('count');<\/code><\/pre>\n\n\n\n<p>We may wish to store in the session for the next request. We may use the <strong>flash <\/strong>method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request->session()->flash('status', 'Task was successful!');<\/code><\/pre>\n\n\n\n<p>To remove a piece of data from the session. We may use <strong>forget <\/strong>method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request->session()->forget('name');<\/code><\/pre>\n\n\n\n<p>We may wish to remove multiple keys:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request->session()->forget(['name', 'status']);<\/code><\/pre>\n\n\n\n<p><strong>The Global Session Helper<\/strong><\/p>\n\n\n\n<p>You may also use the global session PHP function to retrieve and store data in the session. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Route::get('\/home', function () {\n\/\/ Retrieve a piece of data from the session...\n  $value = session('key');\n\n\/\/ Specifying a default value...\n  $value = session('key', 'default');\n\n\/\/ Store a piece of data in the session...\n  session(['key' => 'value']);\n});<\/code><\/pre>\n\n\n\n<p>By Ami<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9770\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9770\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Laravel Session\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9770\" data-text=\"Laravel Session\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9770\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9770\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Sessions provide a way to store information about the user across multiple requests. Session configuration fil [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9770"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9770"}],"version-history":[{"count":4,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9770\/revisions"}],"predecessor-version":[{"id":9775,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9770\/revisions\/9775"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}