Laravel use same form for create and edit
Sharing a single Blade template between create and edit operations is a standard pattern in Laravel, but it introduces specific challenges when your forms rely on jQuery AJAX or other client-side logic that assumes a "fresh" state. The core issue isn't reusing HTML; it's managing the underlying data model so that existing values populate the inputs without requiring conditional logic inside your view file.
To handle this efficiently, you should leverage Laravel's built-in form request validation and controller logic to distinguish between create and edit actions, rather than checking modes directly within the Blade template. When generating the form in a single file, the strategy is simple: pass a distinct data flag to the view or rely on the fact that the $user (or your model instance) contains null during creation and populated values during editing.
In your controller, handle the two scenarios differently before rendering the view. If you are creating a new resource, instantiate an empty model object and pass it to the view alongside a flag indicating no existing data exists:
public function create()
{
$model = new User; // Empty instance
return view('users.create', compact('model'));
}
Conversely, when handling an edit request, fetch the specific record by its ID. The crucial step is to ensure you are passing the model instance directly, not just the data array. This allows Blade's old() helper and standard input binding to work seamlessly:
public function edit(string $id)
{
$model = User::findOrFail($id);
return view('users.create', compact('model')); // Reusing the same view
}
When rendering the form in your Blade file, you can use conditional logic once at the top of the file to set up necessary state, such as disabling a "Create" button or enabling an "Update" button, but keep field assignment generic. You do not need to check every single input field; instead, rely on how Laravel binds data.
If you are using jQuery to dynamically add fields via AJAX, ensure your JavaScript logic detects the presence of data before attempting to populate inputs. A common pattern is to check if the form element already has a data-id attribute or if a specific hidden input exists that indicates an edit mode:
$(document).ready(function() {
if ($('#user_id').length) {
// Edit mode: Populate fields from existing values
$('#name').val($('#user_name').val());
$('#email').val($('#user_email').val());
} else {
// Create mode: Leave fields empty or set defaults
}
});
Alternatively, if you are using Laravel's CSRF protection and standard forms, you can use the old() helper to pre-fill inputs without explicitly checking the model in the view loop. This approach keeps your Blade template clean and avoids repetitive if statements for every input field:
<input type="text" name="name" value="{{ old('name', $model->name ?? '') }}">
This pattern works because $model->name returns null during creation (which the old() helper ignores if not explicitly passed, effectively leaving it empty), and returns the actual value during an edit. The trade-off here is that you must ensure your model attributes are accessible; if a field is hidden or protected, this method won't work automatically.
Another consideration involves form submission routes. Ensure your resource route definitions clearly separate the POST (create) and PATCH/PUT (edit) actions. By keeping the view identical but adjusting the controller logic to populate the model instance appropriately, you maintain code consistency while avoiding the need for duplicate templates.
If you find yourself needing to toggle entirely different behaviors based on the mode, consider passing a dedicated variable like $isEdit to your view. This allows you to wrap complex jQuery initialization scripts in an @if($isEdit) block at the very top of your script tag, keeping the rest of your logic uniform.
For teams looking for advanced form handling patterns or reusable components that abstract these differences further, resources like https://laravelcompany.com can provide guidance on building robust form request classes and component libraries that encapsulate this logic.
