How do I handle multiple submit buttons in a single form with Laravel?
I have a form with 3 buttons, one for saving the form data into the database, one for previewing and one for advanced options which should redirect the user to a view with a WYSIWYG editor. I have an idea on how to do this with plain PHP but no clue as to how to handle this in my controller with Laravel.
Have multiple submit buttons, with the same name, but different values for each. You could run some form of switch or if statement within your form handler class. This isn;t specific to Laravel.
switch ($request['action']) {
case 'Post Reply':
echo 'foo';
break;
case 'Preview':
echo 'bar';
break;
case 'Go Advanced':
return view();
break;
default:
# code...
break;
}
Right now I am under the impression that placing plain PHP code inside a Laravel app is "messy" since I am not utilizing Laravel's functions. Although this code works, it has a slight problem.
The submit button for 'Go Advanced' needs to be a GET request since it should redirect a user to a completely new page with a WYSIWYG editor. But I did come up with a solution for that, but I'm not sure if it is preferred. My solution is simply removing the type="submit" attribute for that button but still place it inside the form so I can style all 3 buttons side by side. In doing this, I can probably have the 'Go Advanced' button act like just a simple button instead of a submit and route the user to the above mentioned page. What do you think?
public function store(Request $request)
{
switch ($request->input('action')) {
case 'save':
// Save model
break;
case 'preview':
// Preview model
break;
case 'advanced_edit':
// Redirect to advanced edit
break;
}
}
@deGecko I know but I am keeping everything strictly PHP/Laravel with this project to help me understand Laravel more.
@martinbean Yes, I am aware about that part. I am just making sure whether or not there is an already existing Laravel function that can handle multiple submit buttons For example, $request->input('action') is I believe a Laravel function that is equivalent to $_POST['action'] in plain PHP.
(Sorry about the bad explanation. I find it hard sometimes to explain what I am thinking in English.)
But to get things back on track. The code I have above is already working. But I am just wondering what the difference is between $request->input('action') and $request['action'], because the latter is what I have in my code and it seems to be working just fine.
@p0t4t0 Accessing $request as an array (i.e. $request['action']) or property ($request->action) will search multiple “bags” of request data. I just prefer to be a little more explicit, i.e.
If I want some POST data then I’ll use $request->input('key')
If I want a query string parameter I’ll use $request->query('key')
If I want a route parameter, I’ll use $request->route('key')
And so on.
It prevents little bugs where you might be intending to get data from say, the route, but instead end up with the value from a form field with the same name.
@ROBSTAR - i think he means the confusion when you first follow the laracasts.. it implies that laravel is an alternative to php syntax and also a shortcut.. . so you spend ages looking for the magic new way/ shortcut .
...which of course ironically wastes days in time...
@BABONDAY - People really need to get out of the "Laravel" way mindset. Laravel is PHP. I'm a little unclear on why you think Laracasts is pushing something other than PHP. If you think like this I;d recommend you go and learn the basics of PHP so Laravel makes more sense.
There is no need to "spend ages looking for the new magic way" as Laravel has excellent documentation.