Submitting a Form with Laravel
When building an edit page for posts, you might want to move away from the Form helper and write raw HTML forms to understand the underlying mechanics better. However, switching from the blade directive {!! Form::open()!!} to manual <form> tags introduces specific requirements regarding how routes are registered and how actions are defined in the controller.
The core issue arises when you attempt to define the action attribute of a form using a dynamic route helper while simultaneously passing an array containing both the method and an ID parameter. In your example, you used:
<form action="{!! Route::post('/posts', ['PostsController@update', $post->id]) !!}" method="POST">
This approach triggers an error in Blade because Route::post() returns a Illuminate\Routing\Route object, not a string. Blade cannot automatically convert this complex routing object into the string required for the HTML action attribute. Consequently, you encounter the fatal error: Object of class Illuminate\Routing\Route could not be converted to string.
To resolve this, you must generate the actual URL string that points to your route before passing it to the form action. Laravel provides the route() helper function specifically for this purpose. This helper takes a named route or a route signature and returns the fully qualified URL string.
Here is the corrected implementation using the route() helper:
<form action="{!! route('posts.update', $post->id) !!}" method="POST">
{{-- Optional CSRF token if not included in Blade directives --}}
@csrf
</form>
However, there is a critical nuance often overlooked when creating update forms manually. Standard HTML forms only support GET and POST methods natively. To perform an update or delete operation, you typically need to send a PUT (or PATCH) request. Since browsers do not allow changing the form's method attribute directly in the standard way without JavaScript, the convention is to submit a POST request but include a hidden input that tricks the server into treating it as a PUT.
You mentioned using <input name="_method" type="hidden" value="PUT">. This is correct and necessary for your manual form. When Laravel receives the request in your PostsController@update method, it will automatically look for this _method field and process the request as a PUT request if it exists.
Your controller logic appears sound based on the provided snippet. The route definition Route::resource('posts', 'PostsController') automatically registers an update method that accepts the ID as the second argument. By using the route() helper with the name generated by your resource route (posts.update) and passing the $post->id, you align your form submission perfectly with the controller's expectation.
One common pitfall when replacing Laravel's Form helpers is forgetting to include the CSRF protection token manually. If you are not using {!! Form::open()!!}, which handles this automatically, you must explicitly add @csrf within your Blade template. Without this, Laravel will reject the POST request with a "CSRF token mismatch" error before it even reaches your controller logic.
<form action="{!! route('posts.update', $post->id) !!}" method="POST">
@csrf
<input name="_method" type="hidden" value="PUT">
<input type="text" name="title" value="{{ old('title', $post->title) }}">
<textarea name="body">{{ old('body', $post->body) }}</textarea>
</form>
By generating the URL string correctly and ensuring both CSRF protection and the _method field are present, your form will submit successfully to the PostsController@update method. This approach gives you full control over the HTML structure while maintaining Laravel's robust routing and security mechanisms.