A
A
Andrey2016-11-25 17:18:42
Laravel
Andrey, 2016-11-25 17:18:42

Why is flash data not saved?

The problem is that when saving (the controller's store method), the message is saved to the session ( flash()->success('Location saved') ), but after redirecting to the index method, this message is no longer there. The same thing happens if you save data in flash.
There are no superfluous redirects, I checked it by writing to the log every time I contacted the service provider, there were two entries in the log (the first - saving, the second - after the redirect).
Routes:

Route::group([
  'prefix' => config('chunker.admin.prefix', 'admin'),
  'namespace' => 'Admin',
  'middleware' => ['admin']
], function () {

  $dir = __DIR__ . '/routes/admin';
  $files = array_slice(scandir($dir), 2);

  foreach ($files as $file) {
    require_once $dir . '/' . $file;
  }

});

// Роут находящийся в routes/admin
Route::group([
    'prefix' => 'places'
], function () {

    Route::get('list', [
        'uses'  => '[email protected]',
        'as'    => 'admin.places',
    ]);

    Route::put('store', [
        'uses'  => '[email protected]',
        'as'    => 'admin.places-store',
    ]);

});

Controller:
function index() {
    $this->authorize('places.view');

    $places = Place::get();

    return view('admin.places', compact('places'));
}


function store(Request $request) {
    $this->authorize('places.edit');

    $data =$request->all();
    $data['latitude'] = (float)str_replace(',', '.', $data['latitude']);
    $data['longitude'] = (float)str_replace(',', '.', $data['longitude']);

    Place::create($data);

    flash()->success('Место сохранено');

    return redirect()->route('admin.places')->with('test', '123');
}

View:
@php( dd( session()->all() ) )
PS laravel - 5.2.45

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrzej Wielski, 2016-11-25
@wielski

What's the problem with doing this?
And on the page:

@if($success = session('success'))
   {{ $success }}
@endif

A
Alexander, 2016-11-25
@kentuck1213

Session::all()

S
Sergey Semenko, 2016-11-25
@abler98

You also need to add middleware - web:

Route::group([
  'prefix' => config('chunker.admin.prefix', 'admin'),
  'namespace' => 'Admin',
  'middleware' => ['web', 'admin'] // Вот тут
], function () {

  $dir = __DIR__ . '/routes/admin';
  $files = array_slice(scandir($dir), 2);

  foreach ($files as $file) {
    require_once $dir . '/' . $file;
  }

});

A
Andrey, 2017-06-27
@andreybold

For those who are interested, I found a solution. More precisely, inattention and inexperience with laravel. It turns out that the web intermediary was hung twice on this route and similar routes, and during processing, the session was recreated, so the response was already a new, clean session in which there was no data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question