Clean way to inject old input

Published 1 year ago by BrianDillingham

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'));
1 year ago (37,355 XP)

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

It will solve throw errors.


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


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>

I use laravel form collection is much easier.

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

//include form fields here

{!! Form::close() !!}
1 year ago (472,345 XP)

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

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


    <form class="form-horizontal" role="form" method="POST" action="/admin/supplier/{{ $supplier->id }}">
    {{ method_field('PATCH') }}
    <form class="form-horizontal" role="form" method="POST" action="/admin/supplier">

Personally I do this...

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

I use so;

 <input type="text" name="slug" value="{{ old('slug',  isset($category->slug) ? $category->slug : null) }}" class="form-control">

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

8 months ago (472,345 XP)

@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

                        <form method="post" action="/speaker/{{ $speaker->id }}">
                        <form method="post" action="/speaker/store">

//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


how do i handle the file input field when editing ?

6 months ago (472,345 XP)


how do i handle the file input field when editing?

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


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.


@Snapey Thanks i discovered it was not possible so i implemented what @willvincent suggested

Sign In or create a forum account to participate in this discussion.