Laravel multiple submit button with different actions

On the form or view

<button type="submit" class="btn btn-primary" name="submit" value="submit">Save & Go BACK</button>
<button type="submit" class="btn btn-primary" name="save" value ="save">Save Note</button>

On the controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Note;
use App\Card;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;

class NotesController extends Controller
{
public function store(Request $request, Card $card){
$card->addNote(new Note($request->all()));
return back();
}

public function edit(Note $note){
return view('note.edit', ['note' => $note]);
}

public function update(Note $note, Request $request){
if(Input::get('submit')) {
$note->update($request->all());
return redirect('/card/'.$note->card_id); //save and go back to card

}else if(Input::get('save')){
         $note->update($request->all()); //save and stay on the page
         return back();
      }
   }
}

1 Comment

  1. Patrick Tobias
    September 21, 2018

    Does this work with Laravel 5.6 ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top