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

clickweb's avatar

What is the best way to use same form for create and edit blade template in laravel 6?

I would like to use one form for creating and editing is any best way to implement it? So I don't have to make changes in both blade templates.

0 likes
13 replies
experimentor's avatar

I use vue to make the input elements components which take old_input as optional components. Form action can change according to the requested route.

sauravs012's avatar

use form model binding so you can easily populate a form's fields with corresponding value.

createOrUpdate.blade.php

@if(isset($user))
    {{ Form::model($user, ['route' => ['updateroute', $user->id], 'method' => 'patch']) }}
@else
    {{ Form::open(['route' => 'createroute']) }}
@endif

    {{ Form::text('fieldname1', Input::old('fieldname1')) }}
    {{ Form::text('fieldname2', Input::old('fieldname2')) }}
    {{-- More fields... --}}
    {{ Form::submit('Save', ['name' => 'submit']) }}
{{ Form::close() }}

So, for example, from a controller, I basically use the same form for creating and updating, like:

// To create a new user

public function create()
{
    // Load user/createOrUpdate.blade.php view
    return View::make('user.createOrUpdate');
}

// To update an existing user (load to edit)
public function edit($id)
{
    $user = User::find($id);
    // Load user/createOrUpdate.blade.php view
    return View::make('user.createOrUpdate')->with('user', $user);
}

Reference: https://stackoverflow.com/questions/22844022/laravel-use-same-form-for-create-and-edit

Snapey's avatar

dont use that Form library, you will regret it in the long run

1 like
getupkid203's avatar

@Snapey Very cool. Not a fan of persisting the data in the Form Request, but the solution for re-using the same view is nice!

Methmi's avatar

Cannot I do both updates and create in two different tables using a single form(I need to update the state when I select one particular data from the dropdown list)

Snapey's avatar

@keithmclaughlin

  • makes you forget how to create basic form elements
  • Uses hard to remember parameter passing (was the class the third parameter or the fourth)
  • redundant now that we can easily create components in Laravel 7
4 likes
ariaceleste's avatar

Shoutout to @snapey for taking the time to write this. It's still useful >2 years later.

Please or to participate in this conversation.