API Redirect Flash Message, message not showing - php

I create an API that work like punch card machine. So when the GET API is requested, it should the result if access granted or access denied on login screen.
I'm running Laravel 5.7 with PHP 7.2. I already dump the session() variable, only cross ref token is shown.
api.php
Route::get('/{member_id}', 'ApiController#entry');
ApiController.php
public function entry($member_id)
{
$user = User::where('member_id', $member_id)->first();
if ($user == null)
return redirect('login')->with('api_error','Access Denied');
else
{
return redirect('login')->with('api_success','Entry Recorded');
}
}
login.blade.php
#if (session('api_success'))
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
{{ session('api_success') }}
</div>
#endif
#if(session('api_error'))
<div class="alert alert-danger alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
{{ session('api_error') }}
</div>
#endif
Nothing is output after run the api link.
Extra Info : the login are scaffolding from php artisan make:auth

Related

How to show messages on Laravel 9?

Application doesn't show messages as it should.
I tried returning back with message in RoleController. Redirecting works fine, but I can't see any messages. There is multiple functions like this, none of them show messages.
public function store(Request $request)
{
$validated = $request->validate(['name' => ['required', 'min:3']]);
Role::create($validated);
return back()->with('message', 'Role created successfully.');
}
And this code below is in admin.layout blade.
#if (Session::has('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
{{ Session::get('message') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
I googled this many many times but can't find a solution. This worked fine month ago, but when I continued with this yesterday, it suddenly stopped working. Php version hasn't changed, it's 8.0.2. I also tried flash-messages with no help.
Sorry, not a native english speaker.
edit / I have also cleared cache with php artisan.
try this
In Controller
return redirect()->route('admin.listing')->with('message', 'Role created successfully.');
In Blade file
#if (Session::has('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
{{ Session::get('message') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
I think you can try this...
#if ($message = Session::get('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
<strong>{{ $message }}</strong>
</div>
#endif
Please try one time with this one
return redirect()->back()->with('message', 'Role created successfully.');
Now it's working. I needed to modify Kernel.php and remove \Illuminate\Session\Middleware\StartSession::class, under the protected $middlewareGroups.

Laravel 8 Redirect back with message not working in blade template

I am using Laravel v8.16.1 and PHP v7.3.1 when I am trying to get a message in the blade template.
#if(session('error'))
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h5><i class="icon fas fa-ban"></i> Success!</h5>
{{ session('success') }}
</div>
#endif
Using the below code, it's working for me.
$req->session()->flash('error', 'Invalid Username or Password');
return view('admin.views.login');
However, it's not the right way, and when I am trying to redirect back using the below code, then it's not working
return redirect()->back()->with('error', 'Invalid Username or Password');
Please help what is missing or what is wrong?
You can use to show errors
#if(count($errors) > 0 )
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<ul class="p-0 m-0" style="list-style: none;">
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
And the redirect code would be
return redirect()->back()->withErrors(['errors' => 'There is some error in the details.']);

Redirect to login page with message not working in laravel 5.3

When registering for external link, I want to dislay a message :
if (!Auth::check()){
Session::flash('message', trans('msg.please_create_account_before_playing', ['tournament' => $tournament->name]));
return redirect(URL::action('Auth\LoginController#login'));
}
In my login page, I have:
#if (Session::has('message'))
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
{{ Session::get('message') }}</div>
#endif
I checked in my login page, with laravel debugbar, and there is no message var in session.
But it never shows up...
Any idea why it is not working?
Try dd(Session::has('message')); within the login method on your LoginController. I suspect this will return true, if so assign it to a variable of message then pass that variable to the view.
Along the lines of:
public function login() {
$message = Session::has('message') ? Session::get('message') : '';
return view('login', compact('message'));
}
Then in your view:
#if (!empty($message))
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
{{ Session::get('message') }}</div>
#endif
If the above does not work, I suspect this may be related to middleware.

Laravel redirect with session message does not always persist to my request

I have an Admin tool built with Laravel where users can perform certain actions on individual records.
The way the code is built I am performing a post to a URL, executing the necessary code, updating the database and then redirecting back with a session message.
Controller Code:
return Redirect::back()->with("msg_success", "SMS has been sent!");
Now the page that gets rendered from the redirect has a check at the top to display the session message if it exists.
View Code:
#if (Session::has('msg_success'))
<div class="col-lg-10">
<div role="alert" class="alert alert-success alert-dismissible fade in">
<button aria-label="Close" data-dismiss="alert" class="close" type="button"><span aria-hidden="true">×</span></button>
<strong>Success! </strong>{{ Session::get('msg_success') }}
</div>
</div>
<div class="clearfix"></div>
#endif
For some reason, sometimes the message is displayed and other times it is not. I cant for the life of me figure out why. Could it be middle ware that is intercepting the request and then performing another request and it is getting destroyed? I do not see anything in there that looks like it would cause this.
Any ideas would help. I thought about using a URL parameter when redirecting like
return Redirect::to('explicitURL?success=whateverthesuccesmessageis')
But I would prefer not do this as my action is shared among different URL's.
In your controller add this:
Session::flash('msg_success', 'SMS has been sent!')
return Redirect::back();
And in the View:
#if (Session::has('msg_success'))
<div class="col-lg-10">
<div role="alert" class="alert alert-success alert-dismissible fade in">
<button aria-label="Close" data-dismiss="alert" class="close" type="button"><span aria-hidden="true">×</span></button>
<strong>Success! </strong>{{ Session::get('msg_success') }}
</div>
</div>
<div class="clearfix"></div>
#endif

Laravel, im using with('message','To send a message') doesn't work

I'm trying to send a message when I created an user but I don't get anything..I'm using laravel 5.2
In my controller got this
return redirect('/usuarios')->with('message','store');
in my view I got this
<?php $message=Session::get('message')?>
#if($message == 'store')
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Usuario creado exitosamente
</div>
#endif
The redirect works perfectly but the message doesn't appear.
Not sure if this fixes the problem, but in Laravel 5.2 you can use this syntax within Blade:
#if(session('message') && session('message') == "store")
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Usuario creado exitosamente
</div>
#endif
Also, please provide use the route to /usuarios and the function that belongs to this route.
I tested and it work
#if(Session::get('message') == "store")
sdlsdklskdl
#endif
Javascript Alert solution laravel 5.2
Route::group(['middleware' => ['web']], function () {
Route::get('/','FrontController#index');
Route::get('contacto','FrontController#contacto');
Route::get('reviews','FrontController#reviews');
Route::get('admin','FrontController#admin');
Route::resource('usuario','UsuarioController');
});

Categories