Check if session key exists in Laravel 5.1? - session

I'm trying to check it a session key has already been set inside a controller. The documentation states that it is possible to check if an item exists in an array and that's it.
http://laravel.com/docs/5.1/session

you can use
if($request->session()->has('key'))
{
}

Has pointed out by #DavidDomain probably the best way of doing it is to use
if(Session::has('...'))
Worked like a charm for me.

you can use Session::has('YOUR_SESSION_KEY') in both blade and controller
controller ex:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
class add_classController extends Controller
{
public function index(){
if (Session::has('YOUR_SESSION_KEY')){
// do some thing if the key is exist
}else{
//the key does not exist in the session
}
}
}
blade ex:
#if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}}
#else
{{-- session key dosen't exist --}}
#endif

You can do this
if(Session::has('your_key')){
return $next($request);
}

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

Or shorter, without using the Session Facade: if(session()->exists('key_here'))

This works for me
#if (session()->has('hasCoupon'))
[{{ Session::get('hasCoupon')['name'] }}]</span>
#endif

#if ( session::has('message')
{{ session::get('message') }}
#endif
or
#if (session->has->('message'))
{{session->
#endif

Try this new laravel syntax
#if(session()->has('success'))
//rest of code here
#endif

Related

condition does not work on blade laravel 5.2

I want to load the panel if the user is logged in
and if the client is the master
But both files are loaded at runtime.
It does not run like a bet!
#if(Auth::check())
#extends('panel')
#else
#extends('master')
#endif
What do you want? To have different layouts for logged user and not logged one?
#extends(Auth::check() ? 'panel' : 'master')
You can't use two extends.
Two extends generate compiled views with code
<?php if(Auth::check()): ?>
<?php else: ?>
<?php endif; ?>
You can see, that extends are not here. But in the end of it -
<?php echo $__env->make('panel')... ->render();
<?php echo $__env->make('master')... ->render();
That's why use see them both.
Try use:
auth()->check(), \Auth::check(), or remove all views in /var/www/html/laravel-master/storage/framework/views/.
If Auth::check() does not work, use Illuminate\Support\Facades\Auth::check() instead:
#if(Illuminate\Support\Facades\Auth::check())
#extends('panel')
#else
#extends('master')
#endif
Edit your extends definition :
Try this
#extends(auth()->check() ? 'panel' : 'master')
You should try this:
#extends(isset(Auth::user()->id) ? 'panel' : 'master');

Laravel 5.4 Chaining to display Auth data

I have this in my master blade
<?php $user=auth()->user() ?>
{{ $user->id }}
Im using this code to display the ID of my user detail,
Can you suggest a way to remove <?php ?> or atleast a cleaner approach on this auth ?
Just use id() method:
{{ auth()->id() }}
If you need some other property or related data to display, use user() to get current user object:
{{ auth()->user()->name }}
Maybe you can do something like :
{{ auth()->user()->id }}
or if you have Laravel Auth
use Illuminate\Support\Facades\Auth;
Auth::id();
You can try above suggested answered as cleaner approach, but if you want to remove <?php ?>, you can try below code:
{{--*/ $user=auth()->user() /*--}}
I recommend to use above answered cleaner approach.
if you want to store user to variable initialize it on #php. For example
#php($user = auth()->user())
{{ $user->id }}

Auth::check() worked in controller but not working in view?

I used Auth::attempt() to log user:
if (Auth::attempt(['email' => $email, 'password' => $password], true)) {
return redirect('/');
}
else{
return 'wrong email or pass';
}
Then I reload browser and call function:
if (Auth::check()) {
return 1;
}
And this return 1;
But when I call:
#if (\Illuminate\Support\Facades\Auth::check())
echo '<label style="width: 20%;color: #ff3f41;margin-left: 10px;">login</label>';
#else
echo '<label style="width: 20%;color: #ff3f41;margin-left: 10px;">nothing</label>';
#endif
This print nothing.
Any helps. Thanks.
instead of
#if (\Illuminate\Support\Facades\Auth::check()) ...
try to use
#if(Auth::Check())
This should work. you dont need to call method by its full path in Blade template
#if(Auth::Check())
<label style="width: 20%;color: #ff3f41;margin-left: 10px;">login</label>
#else
<label style="width: 20%;color: #ff3f41;margin-left: 10px;">nothing</label>
#endif
No need namespace in view Because it is defined in Facades, Check it in config/app.php
And if you use brackets and echo in blade You need to wrap them in <?php tag //code echo 'something' ?>
Laravel way!
If you need to echo something in View use {{ }} or {!! !!} The difference {{ }} escape HTML tags, {!! !!} doesn't escape HTML
goto App/Providers/AppServiceProvider.php in boot(){ } section write this line view()->share('key', 'my name is Rome');
using this you will be able to globally access "key" variable value in all off your templates
eg: see below
<?php
namespace App\Providers;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
view()->share('key', 'my name is Rome');
}
}

laravel login link with return back redirection

I create main menu with login link for quest users.
#if(Auth::guest())
<li>Login</li>
#endif
What is the best way to make back redirection after login?
Now I made the next crutch:
// menu.blade.php
#if(Auth::guest())
<?php
if (Route::getCurrentRoute()->getName() != 'auth::login')
Session::put('login_back_url', Request::url());
?>
<li>Login</li>
#endif
// AuthController.php
public function __construct()
{
...
if (Session::has('login_back_url'))
$this->redirectPath = Session::get('login_back_url');
else
$this->redirectPath = route('home');
...
}
You can use:
use Redirect;
Redirect::back();

Laravel - How to a try/catch in blade view?

I would like to create a way to compile in a view, the try/catch.
How can I do this in Laravel?
Example:
#try
<div class="laravel test">
{{ $user->name }}
</div>
#catch(Exception $e)
{{ $e->getMessage() }}
#endtry
You should not have try/catch blocks in your view. A view is exactly that: a representation of some data. That means you should not be doing any logic (such as exception handling). That belongs in the controller, once you’ve fetched data from model(s).
If you’re just wanting to display a default value in case a variable is undefined, you can use a standard PHP null coalescing operator to display a default value:
{{ $user->name ?? 'Name not set' }}
You are probably doing something wrong if you need a try..catch in your view. But that does not mean you should never do it. There are exceptions to every rule.
So anyway, here is the answer:
Put raw PHP in your view by using <?php ?> or #php #endphp tags. Then put your try..catch in the raw PHP.
#php
try {
// Try something
} catch (\Exception $e) {
// Do something exceptional
}
#endphp
while i agree with #areed that something is wrong if you have to use a try...catch, there are weird cases where a wrong approach can lead you down that path. One instance is using the same blade to house a paginate() and a get() response. Well, this might help a little.
<?php try{ ?>
//try to show something
{{ $products->links() }}
<?php }catch(\Exception $e){ ?>
// show something else
<?php } ?>

Resources