Hmmm, Let's Understand what it means
When you're building a website, users might skip filling out certain fields in forms. But instead of leaving those fields empty, wouldn’t it be nice to give them some default values? That’s where Laravel's mergeIfMissing
comes in handy!
This little helper adds default values to form inputs only if the user hasn’t provided them. Let’s dive into two practical examples, so you can see how it works in action.
Example 1: User Profile Form
Let’s say you’re building a profile page where users can:
- Enter their bio (a short description of themselves).
- Upload a profile picture.
But what if they leave these fields blank? I know people like me never have anything to write about myself. :-D, You don’t want their profile to look incomplete. Instead, you can set a default bio and profile picture.
Code Example
public function updateProfile(Request $request)
{
// We set default values if user leave out bio and profile_picture field
$request->mergeIfMissing([
'bio' => 'Oh Yeah, I left my bio empty, hehe',
'profile_picture' => 'megamind-head.png',
]);
// Use the data filled by user (with defaults applied)
$userData = $request->all();
return response()->json($userData);
}
Real-Life Input and Output
User Input:
{
"name": "Akande Joshua",
"bio": "",
"profile_picture": null
}
Here, I have entered my name but left the bio and profile_picture fields blank or as null
.
Expected Output:
{
"name": "Akande Joshua",
"bio": "Oh Yeah, I left my bio empty, hehe",
"profile_picture": "megamind.png"
}
With mergeIfMissing
, Laravel automatically fills in the missing values without touching the user’s name
cos its already filled.
Example 2: User Preferences Form
Let’s say your app has a settings page where users can choose:
- Whether to receive newsletters (
receive_newsletters
). - Whether to get notifications (
receive_notifications
).
If the user skips these fields, you might want to assume they don’t want these features (set them to false
by default), Even me don't like newsletters.
Code Example
public function updateSettings(Request $request)
{
// Provide default values for missing preferences
$request->mergeIfMissing([
'receive_newsletters' => false,
'receive_notifications' => false,
]);
// Use the settings data
$settingsData = $request->all();
return response()->json($settingsData);
}
Real-Life Input and Output
User Input:
{
"receive_notifications": true
}
Here, the user wants to get notifications but didn’t specify if they want newsletters.
Expected Output:
{
"receive_newsletters": false,
"receive_notifications": true
}
Laravel only fills in the missing newsletter preference while keeping the user’s choice for notifications intact.
Why Use mergeIfMissing
?
- User-Friendly Defaults: Ensures missing fields have sensible defaults.
-
Clean Code: You don’t need to write multiple
if
checks to see if fields are empty. - Flexible: Doesn’t overwrite what the user has already provided.
Therefore?
With mergeIfMissing
, you can build smarter, more robust forms in Laravel. Whether it’s filling in default bios or making sure preferences are complete, this method saves time and makes your app feel polished.
Next time you're building forms, think about how you can use this to improve the user experience. Your users (and your future self) will thank you! 😊
Top comments (0)