Laravel redirect back with () message

Próba przekierowania do poprzedniej strony z wiadomością, gdy wystąpi błąd krytyczny.

App::fatal(function($exception)
{
    return Redirect::back()->with('msg', 'The Message');
}

W widoku próby uzyskania dostępu do msg z

Sessions::get('msg')

Ale nic nie jest renderowane, czy robię coś złego tutaj ?

Author: Ketan Akbari, 2013-11-07

12 answers

Try

return Redirect::back()->withErrors(['msg', 'The Message']);

I w Twoim widoku zadzwoń do tego

@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
 132
Author: giannis christofakis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-03 13:03:49

Laravel 5

Controller

 return redirect()->back()->with('success', ['your message,here']);   

Ostrze:

@if (\Session::has('success'))
    <div class="alert alert-success">
        <ul>
            <li>{!! \Session::get('success') !!}</li>
        </ul>
    </div>
@endif
 57
Author: Ketan Akbari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-06 04:22:44

Alternatywnym podejściem byłoby

Controller

Session::flash('message', "Special message goes here");
return Redirect::back();

Widok

@if (Session::has('message'))
   <div class="alert alert-info">{{ Session::get('message') }}</div>
@endif
 49
Author: Rick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-21 21:56:22

W Laravel 5.4 zadziałało dla mnie:

return back()->withErrors(['field_name' => ['Your custom message here.']]);
 14
Author: haakym,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-03 13:06:13

Masz błąd (błąd ortograficzny):

Sessions::get('msg')// an extra 's' on end

Powinno być:

Session::get('msg')
Myślę, że teraz powinno zadziałać.
 13
Author: bumerang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-13 10:33:09

Po prostu ustaw wiadomość flash i przekieruj na powrót z funkcji kontrolera.

    session()->flash('msg', 'Successfully done the operation.');
    return redirect()->back();

A następnie możesz pobrać wiadomość w pliku view blade.

   {!! Session::has('msg') ? Session::get("msg") : '' !!}
 6
Author: Majbah Habib,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-25 08:58:13

Przestałem pisać to dla laravel na rzecz pakietu Laracasts który obsługuje to wszystko dla Ciebie. Jest naprawdę łatwy w użyciu i utrzymuje kod w czystości. Istnieje nawet laracast, który obejmuje jak go używać. Wszystko co musisz zrobić:

Przeciągnij pakiet przez Composer.

"require": {
  "laracasts/flash": "~1.0"
}

Dołącz usługodawcę do app/config / app.php.

'providers' => [
  'Laracasts\Flash\FlashServiceProvider'
];

Dodaj alias fasady do tego samego pliku na dole:

'aliases' => [
  'Flash' => 'Laracasts\Flash\Flash'
];

Ściągnij HTML do widok:

@include('flash::message') 

Po prawej stronie wiadomości znajduje się przycisk zamykania. To zależy od jQuery, więc upewnij się, że jest dodany przed bootstrap.

Zmiany opcjonalne:

Jeśli nie używasz bootstrap lub chcesz pominąć dołączenie wiadomości flash i napisać kod samodzielnie:

@if (Session::has('flash_notification.message'))
  <div class="{{ Session::get('flash_notification.level') }}">
    {{ Session::get('flash_notification.message') }}
  </div>
@endif

Jeśli chcesz zobaczyć kod HTML pobrany przez @include('flash::message'), możesz go znaleźć w vendor/laracasts/flash/src/views/message.blade.php.

Jeśli chcesz zmodyfikować częściowe zrób:

php artisan view:publish laracasts/flash

The two package widoki będą teraz znajdować się w katalogu' app/views/packages/laracasts/flash/'.

 4
Author: DutGRIFF,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-12 16:32:31

W Laravel 5.5:

return back()->withErrors($arrayWithErrors);

W widoku za pomocą ostrza:

@if($errors->has())
    <ul>
    @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
    </ul>
@endif
 3
Author: Sergio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-25 08:58:35

Stanąłem przed tym samym problemem i to zadziałało.

Controller

return Redirect::back()->withInput()->withErrors(array('user_name' => $message));

Widok

<div>{{{ $errors->first('user_name') }}}</div>
 1
Author: A. Mitani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-16 05:47:33

Dla Laravel 3

Just a heads up on @ Giannis christofakis answer; for anyone using Laravel 3 replace

return Redirect::back()->withErrors(['msg', 'The Message']);

Z:

return Redirect::back()->with_errors(['msg', 'The Message']);
 1
Author: Dev1997,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-08 17:32:11

Dostałem tę wiadomość, gdy próbowałem przekierować jako:

public function validateLogin(LoginRequest $request){
    //

    return redirect()->route('sesion.iniciar')
            ->withErrors($request)
            ->withInput();

Gdy właściwą drogą jest:

public function validateLogin(LoginRequest $request){
    //

    return redirect()->route('sesion.iniciar')
            ->withErrors($request->messages())
            ->withInput();
 0
Author: manix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-22 19:05:52

Laravel 5.6*

Controller

if(true) {
   $msg = [
        'message' => 'Some Message!',
       ];

   return redirect()->route('home')->with($msg);
} else {
  $msg = [
       'error' => 'Some error!',
  ];
  return redirect()->route('welcome')->with($msg);
}

Szablon Ostrza

  @if (Session::has('message'))
       <div class="alert alert-success" role="alert">
           {{Session::get('message')}}
       </div>
  @elseif (Session::has('error'))
       <div class="alert alert-warning" role="alert">
           {{Session::get('error')}}
       </div>
  @endif

Enyoj

 0
Author: The Bumpaster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-04 10:55:56