Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MB's avatar
Level 2

Blade, set input value to old(), $user or empty

Hi,

Is there a better way to handle this?

# Set $company name to old(), if form has been submitted
# Set $company name to $user->company_name (if exists), if form has not been submitted

if(old('company_name') !== null) {
	$company_name = old('company_name');
}elseif(Auth::user()->company_name) {
	$company_name = Auth::user()->company_name;
}else{
	$company_name = '';
}

<input type="text" class="form-control" name="company_name" value="{{ $company_name }}">

Trying to input the value of old() if the form has been submitted. If no form has been submitted, then use settings from user account (if logged in), else empty.

0 likes
8 replies
MB's avatar
Level 2
old('value', 'default')

What happens if the default value is empty? E.g.:

if(Auth::user()->company_name) {
	$company_name = Auth::user()->company_name;
}else{
	$company_name = null;
}

old('company_name', $company_name)
STEREOH's avatar
STEREOH
Best Answer
Level 18

old() has a default value.

so

<input type="text" class="form-control" name="company_name" value="{{ old('company_name', Auth::user()->company_name) }}">
1 like
STEREOH's avatar

If default is null and you don't have an old vaue , the input will be empty.

MB's avatar
Level 2

Thanks, exactly what I was looking for :)

MB's avatar
Level 2

@stereoh What will happen if user is not logged in and no old value is set?

Will the above suggestion from you still work? Or I need to do something like this:

if(isset(Auth::user()->company_name) && Auth::user()->company_name !== null) {
	$company_name = Auth::user()->company_name;
}else{
	$company_name = null;
}

old('company_name', $company_name)
Snapey's avatar

try null coalesce

{{ old('company_name', Auth::user()->company_name ?? '') }}
1 like
STEREOH's avatar

I was under the assumption that your form was behind an auth middleware.

If it isn't the case you should test if you have an auth user.

$company_name = Auth::check() ? Auth::user()->company_name : '';
MB's avatar
Level 2

That is brilliant - I still got a lot to learn it seems :p Thanks again and have a great weekend!

Please or to participate in this conversation.