🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page

Was this helpful?

  1. Laravel

Using the old() helper

Populate a form with user's previous entry, or default data

When we are presenting the user with a dropdown, from which they can choose, for example, a different user, you have to send your form all the possible choices. You can either send the whole model, or just ->pluck('name',$id')

Then you loop over all the contacts setting the select value and name;

<select name="contact">
@foreach($contacts as $contact)

	<option value="{{$contact->id}}">{{ $contact->name }}</option>

@endforeach
</select>

Selecting the previous value

At each iteration of the loop, you want to check if the current contact (the one you are on in the loop) matches the id of the one in the model. We can do this by comparing the current ID with that already stored in the database.

<select name="contact">
@foreach($contacts as $contact)
	<option value="{{$contact->id}}" 
			{{ $contact->id == $order->contact_id ? 'selected' : ''}}>
		  {{ $contact->name }}
   </option>

@endforeach
</select>

The ternary here compares the current contact with the contact on the order.

This is the same as writing

@if($contact->id == $order->contact_id) selected @endif

The above selects the current user on the order, but it does not remember the selection if you change it and then have a validation error.

This is where the old() helper comes in.

<select name="contact">
@foreach($contacts as $contact)

	<option value="{{$contact->id}}" 
		{{ $contact->id == old('contact',$order->contact_id) ? 'selected' : ''}}>
		{{ $contact->name }}
        </option>

@endforeach
</select>

(assuming the select option is named 'contact')

Now, the current option is compared to whatever the old function returns. If it is not set (this is the first time the form is displayed, then the database value from the order is returned. If old data exists then this is being displayed as a result of a validation error, so it returns the value the person selected last time they posted the form.

Syntax old( previous , default )

PreviousUsing Spatie Valuestore to hold frequently accessed settingsNextAlternatives to using Eloquent Accessor

Last updated 4 years ago

Was this helpful?