Skip to main content

Redirecting with Input in Laravel 5.2

I'm posting this because it was somewhat difficult to put together using the Laravel documentation.  It is *there* in the docs, but it is split between different subjects.  So here it is, all put together for your enjoyment:

I generally use redirects either when input fails validation or when the data has been saved and I'm ready to move to the next input page or a "finished" page.  In the case of redirecting with input, we're talking about a validation failure (or some other error).  The related redirect command is pretty straightforward:

return redirect()->back()->withInput();

The part I had trouble deciphering was how to retrieve that information into my blade view or controller.  Note that the input is "flashed" to a session, which means it does not persist between page loads (it is deleted after the page loads).  Fortunately, it turns out to be very simple to retrieve an input:


// Controller: $first_name = old('first_name');
// Blade:
{{ old('first_name') }}

One gotcha is that the regular Blade "or" statement does NOT work ("or" in Blade brackets performs something like empty($var) ? $other_var : $var ).  Instead of outputting the or statement, it outputs a boolean value:


// Outputs "1":
{{ old('first_name') or 'default value' }}
Thankfully, the old() expression doesn't cause an error if the session variable isn't set, so you can use it without an "or" statement.  Or if you need the "or", you can just write your own shorthand expression.



// Outputs $_SESSION['first_name'] or 'Sally':
{{ empty(old('first_name')) ? 'Sally' : old('first_name') }}
That's it!

Comments

Popular posts from this blog

jQuery noUIslider Pip Label Customization

Recently, I was tasked with creating a slider element for a user to select their credit rating, of which the user can select from among: 'Poor', 'Fair', 'Good', 'Excellent' and 'Not Sure'.  Several problems presented themselves here: A drop-down box would be better, but that's not what the requirements specified. I already have several numeric sliders on my page and I want them all to match. I selected the jQuery noUi Slider library to create the numeric sliders. noUi Slider does not support string values - only numeric values. The "pips" (slider scale/labels) also does not support string values. Since the solution involved shifting my mindset, I wanted to document it here for future reference and maybe to help someone facing the same requirements. Here's my solution: Since there is really no numeric correlation to credit score (especially considering one of the options is "Not Sure"), I will just u...

Salesforce Apex: Replacing and/or Removing Apex Classes

Working with code within the constraints of Salesforce's workflow can be a challenge.  First , you have the restriction on changing anything in production.  Second , you have the requirement for 75% or greater unit test coverage.  Third , you have the fact that certain metadata cannot be pushed to production through a changeset (which means the only way to sync record IDs is by re-deploying the development environment from production). Fourth - and this is what I'd like to blog about today - you cannot delete classes from production through the Salesforce UI .  The only way to delete a class from production is to download the class (and its associated metadata file) into the Eclipse IDE (which you have previously set up with the Apex add-on), change the "status" flag to "Deleted", then deploy the changed metadata file to production.  And (here's the kicker)...sometimes it doesn't work because...reasons. So let's start with the workflow of a f...

PHP empty()

The PHP "empty($var)" function (or language construct) is a handy function that I use a lot.  It replaces all of the following checks: isset($var) $var !== 0 $var !== 0.0 $var !== "" $var !== "0" $var !== null $var !== false count($var) > 0 // if array This is particularly useful for integers, but can also be useful for checking strings and floats.  It replaces a bunch of conditions you'd otherwise have to type. I've had some disagreements over my frequent usage of empty().  There are those who believe explicit value checks are important to code readability.  My response is to ask why a longer line of code is any more readable.  There are also those who consider '0' to be non-empty, in which case the use of empty() would not be appropriate.  However, the majority of the time when you use empty(), it will be to check a MySQL ID value, which will (should) always be greater than 0. Consider the following example: if...