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

pavelfilingeri's avatar

I can't get successful or error messages with the redirect!

Hello, i'm a begginer with this framework, im creating a Register for students and for the login page i'm doing a simply page with guards ecc ecc. Also i have always this error, the page works perfectly , but if i put wrong fields i can't receive the error message, it's the same for the successful messages. For example in this part for the email:

public function sendEmail_teacher(Request $request){
   $request->validate([
       'email_studente' => 'required|email',
       'messaggio_da_inviare' => 'required|string|min:10|max:500',
       'nome_studente' => 'required|string',
       'email_professore'=>'required|email',
       'id'=>'required',
   ]);

   $data = [
       'email_studente' => $request->email_studente,
       'messaggio_da_inviare' => $request->messaggio_da_inviare,
       'nome_studente' => $request->nome_studente,
       'email_professore' => $request->email_professore,
   ];
   $id=$request->id;
Mail::to($request->email_professore)->send(new MailStudent_Teacher($data));
sleep(1);
return redirect()->route('student.dashboard')->with('success', 'Email inviata con successo!')};

BLADE FILE


@if (session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif 

ROUTES:

Route::get('send/mail/contact={id}', [SendEmailController::class,'mail_pass'])->name('send.mail_teacher');
Route::post('send/mail/', [SendEmailController::class,'sendEmail_teacher'])->name('send.mail_teacher_due')

How can i fix this issue? Thanks in advance!

0 likes
8 replies
tykus's avatar

A validation failure will redirect back to the previous URL; usually the page with the form; here is where you should display the validation errors, e.g.

<div>
    <label for="email_studente">Email</label>
    <input type="text" id="email_studente" name="email_studente" />
    @error('email_studente')
        <p class="invalid-feedback">{{ $message }}</p>
    @enderror
</div>

The success message should appear based on the code you shared; where is this Blade snippet located in your files?

1 like
pavelfilingeri's avatar

@tykus Thanks for the reply, but my question was for the session, if you see

return redirect()->route('student.dashboard')->with('success', 'Email inviata con successo!')};

When the user sends the email, and with the redirect it goes back into the student.dashboard, i can't see the correct sentence, despite there is the,

@if (session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif 

tykus's avatar

@pavelfilingeri you said that you could not receive errors, so I answered that

but if i put wrong fields i can't receive the error message

I also queried your issue with the success message

The success message should appear based on the code you shared; where is this Blade snippet located in your files?

Is there anything in the Session?

pavelfilingeri's avatar

@tykus Inside the session maybe no, the only stuff that i put is with('success', 'Email inviata con successo!') . And in the blade snippet im trying to see this with the @if (session('success')) . Also in the sessions there aren't others values

tykus's avatar

@pavelfilingeri like I said; it should work based on the information you shared. What does the student.dashboard route look like; what happens inside its Controlller action?

I asked about seeing the Session contents only to understand if it is an issue of the Session, or the UI. You can check in the Blade template using, and checking if success key is there:

@dump(session()->all())
pavelfilingeri's avatar

@tykus this is the dashboard controller

    public function dashboard(){

        if(!Auth::guard('student')->check()){
            return redirect(route('login.student'));
        }


        //Query per ottenere i dati dello studente loggato
        $studente=Auth::guard('student')->user();


        $classe=DB::table('CLASSI')
        ->select('CLASSI.NOME')
        ->join('STUDENTI','STUDENTI.ID_CLASSE','=','CLASSI.ID')
        ->where('STUDENTI.ID','=',$studente->ID)
        ->get();

        $mediaVoti = DB::table('VOTI')
        ->join('STUDENTI', 'STUDENTI.ID', '=', 'VOTI.ID_STUDENTE')
        ->where('STUDENTI.ID', '=', $studente->ID)
        ->avg('VOTI.VALUTAZIONE');

        $professori=DB::table('PROFESSORI')
        ->select('PROFESSORI.*')
        ->join('INSEGNAMENTI','INSEGNAMENTI.ID_PROFESSORE','=','PROFESSORI.ID')
        ->join('CLASSI','CLASSI.ID','=','INSEGNAMENTI.ID_CLASSE')
        ->join('STUDENTI','STUDENTI.ID_CLASSE','=','CLASSI.ID')
        ->where('STUDENTI.ID','=',$studente->ID)
        ->get();


        return view('codischool_dashboardstudent',compact('studente','classe','mediaVoti','professori'));
    }
tykus's avatar

@pavelfilingeri there is nothing here to overwrite the Session. Did you dump the entire contents of the Session in the view like I suggested above?

krisi_gjika's avatar

@pavelfilingeri try like:

@if (session()->has('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif 

Please or to participate in this conversation.