Alert::get() is empty
aspyropoulos opened this issue · comments
Your Environment
- Laravel Version: 8.38.0
- OS and Version: Ubuntu 20
- PHP Version: 8.0.3
- Alerts Version: 5.1.0
Expected behaviour
Following the guide after setting in my Controller Alert::error('error message');
in blade App:get()
should contain the alert.
Actual behaviour
What is is happening is that in blade App:get()
gives and empty array.
My .env file contains session and cache as below:
CACHE_DRIVER=file
SESSION_DRIVER=file
This has been tested in a newly fresh Laravel project.
Am I missing some configuration?
Hello
If i understood it properly, you are setting the alert on a view file. I don't think that would work as expected, you need to define the alert in your controller for example.
Then inside your view you can use the Alert::get()
to render these alerts.
Here's a very basic setup with fresh Laravel 8.
On the routes.php
file you can do something like:
<?php
use Cartalyst\Alerts\Laravel\Facades\Alert;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
dd(Alert::get());
return view('welcome');
});
Route::get('set-alert', function () {
Alert::error('foo');
return redirect('/');
});
Then when you visit the endpoint /set-alert
, for example http://127.0.0.1:8000/set-alert
, it will set the alert and redirect you to /
which will show the alert you've just set.
Sorry if I didn't explain it before @brunogaspar, but I am setting the alert from a controller (not a view) and does not work. I also tried your example and still getting an empty array. If you also tried with a fresh Laravel 8 installation then perhaps it has something to do with the config? Is there any .env variable that it could explain this behaviour? I am using Laravel sail to have the project up and running.
No problem :)
Would be kind to zip this fresh test project you have and send it to me please (you can send to my email directly)? I can have a look and see what might be wrong.
Sure I sent it.
Ok, i saw the mistake.
On your routes/web.php
you're importing the native facade
use Cartalyst\Alerts\Native\Facades\Alert;
But it should be the Laravel one instead
use Cartalyst\Alerts\Laravel\Facades\Alert;
Do that change and it should work as expected.
That made the trick. Thanks!
You're welcome!