1

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:

  1. The regular form works perfectly when I use @method('PUT') inside the form.
  2. I tried adding @method('PUT') in the component file (form.blade.php), but it seems like the component isn't handling PUT correctly.
  3. 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?

  1. Does the @component directive not handle form methods like PUT automatically?
  2. 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!

1
  • What do you mean by it doesn't work? Commented Oct 19, 2024 at 6:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.