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

BrianDillingham's avatar

Clean way to inject old input

This, to me atleast, feels like a clean way to have edit & create views share fields

Shared Input

<input type="text" name="first_name" value="{{ old('first_name', $contact->first_name) }}">

Create & Edit Method - Because $contact isn't defined in create it throws an exception.

Any opinions / thoughts on adding an empty model, making the default value of old() = empty.

public function create()
{
        return view('contact.create')->withContact(new Contact);
}

public function edit($id)
{
        $contact = Contact::find($id);

        return view('contact.edit', compact('contact'));
}
0 likes
11 replies
veve286's avatar

<?php $firstName=isset($contact) ? $contact->first_name : '' ?> <input type="text" value="{{ old('first_name',$firstName )}}" >

It will solve throw errors.

1 like
athulpraj's avatar

try this ....for create pass a false value for $contact

$contact=false;

from the controller to the view and in the view do the following mods

<input type="text" name="first_name" @if($contact)value="{{ old('first_name', $contact->first_name) }}"@endif>
Devmaurice's avatar

I use laravel form collection is much easier.

@if(isset($contact))
{!! Form::model($contact, ['route' => ['contact.update', $contact->id], 'class' => 'form-horizontal']) !!}
@else
{!! Form::open(['method' => 'POST', 'url' => '/contact', 'class' => 'form-horizontal']) !!}
@endif

//include form fields here

{!! Form::close() !!}
1 like
Snapey's avatar

I pass a new model to the edit view. Works fine.

I also test it to include different versions of the form tag

eg;

@if($supplier->exists)
    <form class="form-horizontal" role="form" method="POST" action="/admin/supplier/{{ $supplier->id }}">
    {{ method_field('PATCH') }}
@else
    <form class="form-horizontal" role="form" method="POST" action="/admin/supplier">
@endif
leeovery's avatar

Personally I do this...

<input type="hidden"
          value="{{ isset($listing)
                ? $listing->location()->latitude()
                : old('location.lat') }}"
>
dharav's avatar

can anyone provide code in contact.edit file so that it renders createforam

Snapey's avatar

@dharav not quite sure what you are asking, but I'll give it a shot.

Suppose your edit method in the controller is like this;

public function edit(Request $request, Speaker $speaker)
{
    return view('speaker.edit')->with(compact('speaker'));
}

Then the model being edited is passed to the view.

In the create method of the controller, you can do this;

public function create()
{
    $speaker = new Speaker;
    return view('speaker.edit')->with(compact('speaker'));
}

Both methods use the same view...

Then in the view;

// the form tag needs to be different between create and edit

                @if($speaker->exists)
                        <form method="post" action="/speaker/{{ $speaker->id }}">
                @else
                        <form method="post" action="/speaker/store">
                @endif 

//the input fields can use the old() method to show the model or the previous submission

        <input name="subject" value="{{ old('subject', $speaker->subject) }}" />

So, a single form can edit or create

3 likes
mazinoukah's avatar

how do i handle the file input field when editing ?

Snapey's avatar

@mazinoukah

how do i handle the file input field when editing?

You cannot. You have to ask for the file again.

willvincent's avatar

Not entirely true.. you can't use an 'old' value to populate file input field, that's true.

However, since @mazinoukah's question seems to be about editing, not fixing validation errors and such, you could display the filename and maybe a thumbnail if it's an image, with a 'remove' option (you'd have to implement that functionality), on an edit form.. or skip the remove option and provide a file field so that if a new file is uploaded the previous one is replaced with the new upload... depends how you want it to work really.

Please or to participate in this conversation.