I'm trying to create a form using a Blade component in Laravel, but it doesn't work when using the PUT method. However, when I switch to a regular HTML form, everything works fine. Below is my current setup:
// views/pages/admin/category/edit.blade.php
@extends("templates.admin")
@section("title")
Category Edit
@endsection
@section("content")
@component("organisms.form", ["action" => "/admin/category/{{$category->id}}", "method" => "PUT"])
@slot("fields")
@include(
"atoms.inputs.text-input",
[
"label" => "Category Name",
"id" => "name",
"name" => "name",
"class" => "mb-4",
"value" => $category->name,
"placeholder" => "Novel",
]
)
@endslot
@slot("buttons")
@include("atoms.buttons.anchor-white", ["href" => "/admin/category", "text" => "Back"])
@include("atoms.buttons.indigo", ["type" => "submit", "text" => "Change"])
@endslot
@endcomponent
@endsection
Blade Form Component
// views/organisms/form.blade.php
<form action="{{ $action }}" method="POST">
@csrf
@method("put")
{{-- Error Messages --}}
@if ($errors->any())
@include("molecules.form-error-message", ["errors" => $errors])
@endif
{{-- Fields --}}
{{ $fields }}
{{-- Buttons --}}
{{ $buttons }}
</form>
Working Regular Form Example:
<form action="/admin/category/{{$category->id}}" method="POST">
@csrf
@method('PUT')
@include(
"atoms.inputs.text-input",
[
"label" => "Category Name",
"id" => "name",
"name" => "name",
"class" => "mb-4",
"value" => $category->name,
"placeholder" => "Novel",
]
)
<a href="/admin/category" class="btn btn-white">Back</a>
<button type="submit" class="btn btn-indigo">Change</button>
</form>
What I’ve Tried:
- The regular form works perfectly when I use @method('PUT') inside the form.
- I tried adding @method('PUT') in the component file (form.blade.php), but it seems like the component isn't handling PUT correctly.
- I tried adding @method('PUT') in the parent file (edit.blade.php) through component slot, but still the component isn't handling PUT correctly.
What Could Be the Issue?
- Does the @component directive not handle form methods like PUT automatically?
- Is there something specific I need to add to the Blade component to make it work with methods other than POST?
Any help on why the form component is failing but the regular form works would be appreciated. Thanks!