Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

scala's avatar
Level 1

message not printed in session after redirecting from edit page to view page

In my edit page, after the update button is pressed, I redirected to view page with update message in session variable. But the message is not displayed in the view page. controller code block

 public function create()
{
    return view('pages.book', $this->fetchData());
}

 public function update(Request $request, $id)
{
    $book = Book::find($id);
    $book -> BookName = $request->get('NBookName');
    $book -> BookID = $request->get('NBookId');
    $book -> BookUnitPrice = $request->get('NBookUnitPrice');
    if($book->save())
        {
            return view('pages.book', $this->fetchData())->with('alert-success', 'books updated successfully.');              
        }  
        else
        {
            return redirect()->back()->with('alert-success',$error->getMessage()); 
        }
} 

pages.book session code block

<div class="flash-message">
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
  @if(Session::has('alert-' . $msg))
  <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }}</p>
  @endif
@endforeach
</div>

route code block

Route::get('/book','BookController@create');
Route::post('/book','BookController@store');
Route::get('/book/{id}','BookController@edit');
Route::patch('/book/{id}', 'BookController@update');
0 likes
4 replies
newbie360's avatar

here you are using view()->with('alert-success',.........), alert-success is normal variable, not is session variable

    if($book->save())
        {
            return view('pages.book', $this->fetchData())->with('alert-success', 'books updated successfully.');              
        } 

view()->with() and redirect()->with() is two difference things

view()->with() return $variable

redirect()->with() return session variable

so change the code to use redirect()

return view('pages.book', $this->fetchData())->with('alert-success', 'books updated successfully.');

and after post request (such as store, update), should use redirect()

because if use view(), erveryone can reload the page submit the data again

scala's avatar
Level 1

@newbie360 how to use route code here? I did

return redirect::route('pages.book')->with('alert-success', 'books updated successfully.'); 

getting error as

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'App\Http\Controllers\redirect' not found

and when I did,

return redirect('pages.book')->with('alert-success', 'books updated successfully.'); 

browser is displaying as Sorry, the page you are looking for could not be found. My route code is::

Route::get('/book','BookController@create');
Route::post('/book','BookController@store');
Route::get('/book/{id}','BookController@edit');
Route::patch('/book/{id}', 'BookController@update');

Please help, what is the issue with this.

newbie360's avatar

depends on after stored, where you want redirect to

first, may be change your route list, because the create route look like is for index

and also give a route name

Route::get('/book','BookController@index')->name('book.index'); // also create the index method for display book home page
Route::get('/book/create','BookController@create')->name('book.create');
Route::post('/book','BookController@store')->name('book.store');
Route::get('/book/{id}','BookController@edit')->name('book.edit');
Route::patch('/book/{id}', 'BookController@update')->name('book.update');

ok now, change after stored redirect to book.index

// store() method, here book.index is the route name, not the blade location
return redirect()->route('book.index')->with('alert-success', 'books updated successfully.'); 

before you are hardcored the route uri, from now, use the route name instead,

in the view, use {{ route('book.index') }} {{ route('book.create') }} ...etc

Please or to participate in this conversation.