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

Bole_nbg's avatar

controller is redirecting me to a post route and not the one I wrote

Hello :) not a professional here. I have issue with controller that is not redirecting me to a route I wrote to.

ROUTES (with 'admin' prefix):

Route::get('/emails/inbox', [ 'uses'=>'EmailController@index', 'as'=>'emails.index' ]); Route::get('/emails/create', [ 'uses'=>'EmailController@create', 'as'=>'emails.create' ]);

Route::post('/emails', [
    'uses'=>'EmailController@store',
    'as'=>'emails.store'
]);

I have in form:

CONTROLLER: public function store(Request $request) { $email_id=Session::has('email_id') ? Session::pull('email_id') : null; if($request->send){ $this->sendEmail($request, $email_id); }elseif($request->save){ $this->saveToDrafts($request, $email_id); }else{ return redirect()->back(); } }

protected function saveToDrafts($request, $email_id){
   
    if($email_id){
        //znači dolazimo sa edit. ne pravimo novu instancu nego koristimo postojeći red
        $email=Email::findOrFail($email_id);
        $email->sender=Auth::id();
        $email->receiver=$request->receiver;
        $email->title=$request->title;
        $email->body=$request->body;
        $request->session()->forget('email_id');
        //$attachment=new Attachment();
        $email->save(); 
    }else{
        //znači ne dolazimo sa edit stranice. pravimo novu instancu tj novi red i dajemo is_draft=true
        $email=new Email();
        $email->sender=Auth::id();
        $email->receiver=$request->receiver;
        $email->title=$request->title;
        $email->body=$request->body;
        $email->is_draft=true;
        //  
        $email->save(); 
    }
    Log::info('hey'); // THIS WORKS
    toastr()->success('Email saved to drafts successfully!');
    //return redirect()->action('EmailController@index'); no
    //return redirect('/admin/emails/inbox'); no
    //return redirect()->to('emails.index'); no
//return redirect()->to('/emails/index'); no
    return redirect()->route('emails.index');  

}

I get message 'hey' in the log, row is being saved to the table, but it redirects me to the http://adminlte.test/admin/emails (blank page, such view I don't have), which is the post route and not the one(s) i wrote in the last line. Like it doesn't see return redirect()...

F1 please :)

0 likes
3 replies
Bole_nbg's avatar
Bole_nbg
OP
Best Answer
Level 1

i found the solution... return redirect... needs to be in store method, bellow $this->saveToDrafts() and bellow $this->sendEmail()

Cronix's avatar

@Bole_nbg You have a lot of duplicated code, as well. You could simplify it a lot.

protected function saveToDrafts($request, $email_id){
   
    if($email_id){
        //znači dolazimo sa edit. ne pravimo novu instancu nego koristimo postojeći red
        $email=Email::findOrFail($email_id);   
        $request->session()->forget('email_id');
    }else{
        //znači ne dolazimo sa edit stranice. pravimo novu instancu tj novi red i dajemo is_draft=true
        $email=new Email();
        $email->is_draft=true;        
    }

    // these are being set/saved for both, so they don't need to be in both if/else
    $email->sender=Auth::id();
    $email->receiver=$request->receiver;
    $email->title=$request->title;
    $email->body=$request->body;
    $email->save(); 

    Log::info('hey'); // THIS WORKS
    toastr()->success('Email saved to drafts successfully!');

}

Please or to participate in this conversation.