Reputation: 11
I have a resource named categories on my web.php file with the following content:
Route::prefix('admin')->middleware(['web'])->group(function () {
Route::get('login', [AdminLoginController::class, 'showLoginForm'])->name('admin.login');
Route::post('login', [AdminLoginController::class, 'login'])->name('admin.login.post');
Route::post('logout', [AdminLoginController::class, 'logout'])->name('admin.logout');
Route::middleware(['auth', 'admin'])->group(function () {
Route::resource('categories', CategoryController::class);
Route::post('update-category-status', [CategoryController::class, 'updateStatus'])->name('admin.updateCategoryStatus'); // This belongs to Categories
Route::resource('products', ProductController::class);
Route::get('dashboard', function () {
return view('admin.index');
})->name('admin.index'); // This is a temporary route, the dashboard will be changed later
});
});
In my CategoryController I have the two methods:
public function index()
{
Log::info('In Index Method');
Log::info('Session Data in Index:', session()->all());
$categories = Category::with('parent', 'children')->get();
return view('admin.productCategory.index', compact('categories'));
}
and my destroy method or any other method in this controller:
public function destroy(string $id)
{
$category = Category::find($id);
if ($category && $category->delete()) {
session()->flash('product_deleted', 'Category deleted successfully!');
} else {
session()->flash('product_not_deleted', 'Category was not deleted!');
}
Log::info('AFTER THIS ');
Log::info('Session Data:', session()->all());
session()->flash('product_deleted', 'Category deleted successfully!');
return redirect()->route('categories.index');
}
The index method won't receive the session value. The logger prints the following log:
[2024-07-30 14:43:26] local.INFO: AFTER THIS
[2024-07-30 14:43:26] local.INFO: Session Data: {"_token":"210aCSLtzRf4afNsYuzuY4xsYG1khCMf01YMc52H","url":[],"_previous":{"url":"http://127.0.0.1:8000/admin/categories"},"_flash":{"old":[],"new":["product_deleted"]},"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d":1,"auth":{"password_confirmed_at":1722348879},"product_deleted":"Category deleted successfully!"}
[2024-07-30 14:43:26] local.INFO: In Index Method
[2024-07-30 14:43:26] local.INFO: Session Data in Index: {"_token":"210aCSLtzRf4afNsYuzuY4xsYG1khCMf01YMc52H","url":[],"_previous":{"url":"http://127.0.0.1:8000/admin/categories"},"_flash":{"old":["product_deleted","product_deleted"],"new":[]},"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d":1,"auth":{"password_confirmed_at":1722348879},"product_deleted":"Category deleted successfully!"}
[2024-07-30 14:43:27] local.INFO: In Index Method
[2024-07-30 14:43:27] local.INFO: Session Data in Index: {"_token":"210aCSLtzRf4afNsYuzuY4xsYG1khCMf01YMc52H","url":[],"_previous":{"url":"http://127.0.0.1:8000/admin/categories"},"_flash":{"old":[],"new":[]},"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d":1,"auth":{"password_confirmed_at":1722348879}}
I checked kernel for anything that might be wrong but everything seems alright:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\Localization::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
also the session.php file seems alright:
'driver' => env('SESSION_DRIVER', 'file'),
I will be able to receive the value if lets say it is within index.php like this:
public function index()
{
Log::info('In Index Method');
Log::info('Session Data in Index:', session()->all());
session()->flash('product_deleted', 'Category deleted successfully!');
$categories = Category::with('parent', 'children')->get();
return view('admin.productCategory.index', compact('categories'));
}
Upvotes: 1
Views: 91