Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
What is session in laravel?
Sessions are used to store information about the user across the requests. Session can be configured in the file stored at config/session.php.
Sessions are a way to store data for each HTTP request with a unique session ID.
There are two methods to set the value in the session.
// Global helper function
session(['key' => 'value']);
############## OR #########
// Request class instance
$request->session()->put(['key' => 'value']);
############## OR #########
// Pusing Value to Session Array
$request->session()->push(['key' => 'value']);
Import session in controller
use Illuminate\Support\Facades\Session;
use Session;
Accessing Session Data
To access the session data, we need an instance of session which can be accessed via HTTP request. After getting the instance, we can use the get() method, which will take one argument, “key”, to get the session data.
File name : index.php
// Global helper function
$value = session('key');
br/>
############## OR #########
$data = $request->session()->get('key');
############## OR #########
You can use all() method to get all session data instead of get() method
$data = $request->session()->all();
Storing Session Data
Data can be stored in session using the put() method. The put() method will take two arguments, the “key” and the “value”. or the session helper:
$request->session()->put('key', 'value');
$email = $request->input('email');
$request->session()->put('username',$email);
$username = $request->session()->get('username');
// Via the global helper...
session(['key' => 'value']);
How to print session data on view page
How to strore multiple data on Session
$userdata = array('username'=>'mahtab','userid'=>'mahi786','mobile' =>'7838897299');
$request->session()->put('userkey',$userdata);
how to print print data on view page
File name : index.php
<label>
{{Session::get('userkey')['username']}}
{{Session::get('userkey')['userid']}}
</label>
Deleting Session Data
The forget() method is used to delete an item from the session. This method will take “key” as the argument.
$request->session()->flush(); // it remove the all session data
$request->session()->forget('username'); // it remove the single session data
// Forget a single key...
$request->session()->forget('key');
// Forget multiple keys...
$request->session()->forget(['key1', 'key2']);
Session::forget('your key') // Removes a specific variable
How to check session is exist or not
if (session()->has('userkey')) {
return redirect('home');
}
else {
return redirect('/');
}
if (!$request->session()->exists('user')) {
// user value cannot be found in session
return redirect('/');
}
// Checking if a session value exist
if ($request->session()->has('key') {
//
}
How to Increase Session time in Laravel?
Using .env File
SESSION_LIFETIME=525600
60 * 24 * 365 = 525600
config/session.php
<?php
use Illuminate\Support\Str;
return [
.....
'lifetime' => env('SESSION_LIFETIME', 120),
.....
]
Method 2:- Using Config File
config/session.php
<?php
use Illuminate\Support\Str;
return [
.....
'lifetime' => 1 * (60 * 24 * 365),
]
Flash Data
$request->session()->flash('status', 'Task was successful!');
If you need to keep your flash data around for several requests, you may use the reflash method,
$request->session()->reflash();
If you only need to keep specific flash data, you may use the keep method
$request->session()->keep(['username', 'email']);
Regenerating The Session ID
$request->session()->regenerate();
How to create session key in your controller after login?
$user = Auth::User();
Session::put('user', $user);
$user=Session::get('user');
return $user->name;
How to check session is exist on view page in laravel?
@if (Session::has('user'))
Welcome {{{ isset(Auth::user()->name) ? Auth::user()->name : Auth::user()->email }}}
@else
<script>
window.location.href = '{{url("/")}}';
</script>
@endif
How to check session in blade view file in laravel?
@if(session()->has('qwick'))
@else
@endif
Get All session data in laravel
$data = $request->session()->all();
Print all session data in laravel
If you just want to see contents of session, try dd():
dd(session()->all());
If not, just use this to get all info:
$data = session()->all();
Get session in blade
{{ session()->get('name') }}
Print session data on view page
{{ session('key_name') }}
Get session variable in controller file
$value = Session::get('your key');
Session flash in laravel
use Illuminate\Support\Facades\Session;
Session::flash('message','This is a message!');
then in your view::
@if(Session::has('message'))
<p class="alert
{{ Session::get('alert-class', 'alert-info') }}">{{Session::get('message') }}</p>
@endif
Get Session id in laravel
Session::getId();
store multiple session in laravel
//store multipal data
Session::put('user', ['first_name' => $request->get('first_name'), 'user_role' => Auth::user()->user_role, 'city' => Auth::user()->city]);
//in blade template
<p>
{{Session::get('user')['city']}}
</p>
@foreach (Session::get('user') as $user)
{{$user}}
@endforeach
//condtion check
@if(Session::has('user'))
@foreach (Session::get('user') as $user)
{{ $user }}
@endforeach
@endif
// condition check using ternary operator
{{ (Session::has('user')) ? 45 : 55}}
//delete session
Session::forget('customer');
Update session laravel
$request->session()->push('user.uploadPic', true);
redirect page in view file
<script>
// your "Imaginary javascript"
window.location.href = '{{url("yoururl")}}';
// or
window.location.href = '{{route("myRoute")}}'; //using a named route
</script>