I am trying to achive Single view page form.blade.php that deal with create edit and update function in laravel 11 .
Except the update are working
form.blade.php
<x-app-layout>
<x-create-form-layout
:action="$action"
:method="$method"
>
<x-slot name="title">
{{ isset($category) ? 'Edit Category' : 'Create Category' }}
</x-slot>
<x-slot name="leftInputs">
</x-slot>
<x-slot name="rightInputs">
</x-slot>
<x-slot name="freeSlot">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- Left Column -->
<div class="space-y-4">
<!-- Category -->
<div>
<label for="client" class="block text-gray-700 text-sm font-bold mb-2">Category</label>
<input
type="text"
name="name"
id="category"
required
class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500"
placeholder="Enter Category name"
value="{{ old('name', $category->name ?? '') }}"
>
@error('name')
<p class="text-red-500">{{ $message }}</p>
@enderror
</div>
</div>
<!-- Right Column -->
<div class="space-y-4">
<!-- Code -->
<div>
<label for="gstin" class="block text-gray-700 text-sm font-bold mb-2">Code</label>
<input
type="text"
name="code"
id="code"
required
class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500"
placeholder="Enter Code"
value="{{ old('code', $category->code ?? '') }}"
>
@error('code')
<p class="text-red-500">{{ $message }}</p>
@enderror
</div>
</div>
</div>
</x-slot>
<!-- Buttons -->
<x-slot name="buttons">
<x-submit-button>{{ isset($category) ? 'Update' : 'Save' }}</x-submit-button>
<x-list-button href="{{ route('categories.index') }}">List</x-list-button>
</x-slot>
</x-create-form-layout>
</x-app-layout>
create-form-layout.blade.php
@props(['action', 'method' => 'POST', 'title', 'leftInputs', 'rightInputs', 'listHref' => '#','freeSlot'=>''])
<div class="max-w-6xl mx-auto mt-8">
<form action="{{ $action }}" method="{{ $method }}"
class="bg-white shadow-md rounded-lg p-6 space-y-8 min-h-[35rem]">
@csrf
@if (strtoupper($method) !== 'POST')
{{-- <input type="hidden" name="_method" value="{{ strtoupper($method) }}"> --}}
@method($method)
@endif
<h2 class="text-2xl font-semibold text-gray-700 text-center">{{ $title }}</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- Left Column -->
<div class="space-y-4">
<!-- Category -->
{{ $leftInputs }}
</div>
<!-- Right Column -->
<div class="space-y-4">
<!-- Code -->
{{ $rightInputs }}
</div>
</div>
<div {{ $attributes->merge(['class' => ''])}}>
{{ $freeSlot }}
</div>
<!-- Buttons -->
<div class="flex justify-center space-x-4 mt-6">
{{ $buttons }}
</div>
</form>
</div>
Controller
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$dataProvider = Category::all();
return view('categories.index', compact('dataProvider'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('categories.form', [
'action' => route('categories.store'),
'method' => 'POST',
'category' => null, // No category data for create
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreCategoryRequest $request)
{
Category::create($request->validated());
return redirect()->route('categories.index')->with('success', 'Category created successfully.');
}
/**
* Display the specified resource.
*/
public function show(Category $category)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$category = Category::findOrFail($id);
$route = route('categories.update', $category->id);
return view('categories.form', [
'action' => $route,
'method' => 'PUT',
'category' => $category, // Pass existing category data for edit
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Category $category)
{
logger('Update function reached', ['data' => $request->all()]);
$category->update($request->only(['name', 'code']));
return redirect()->route('categories.index')->with('success', 'Category updated successfully.');
}
public function destroy(Category $category)
{
$category->delete();
return redirect()->route('categories.index')->with('success', 'Category deleted successfully.');
}
}
when i submit the form in the edit state the page redirected to a blank white page with url http://localhost/my-app/public/categories/24?_token=kyfFfJDIG8CjfVKnvqKQKcldrH2d8752m8ZWMNyc&_method=PUT&name=Jumbo&code=n
. and no leads to what happening . I tried logging the update function didn’t reach and no data related in the log .
@dump($action) result "http://localhost/my-app/public/categories/24" // resourcesviews/components/create-form-layout.blade.php
I can’t find the issue is
2
Answers
It seems like your CategoryController@update method isn’t being executed. it could be due to a route mismatch or middleware or authorization issues.
Could you paste your route file?
You also need to enable error debugging in your .env file by setting
APP_DEBUG=true
I think, that’s because in your
edit
method you are using'method' => 'PUT',
and method is directly used in actionaction="{{ $action }}" method="{{ $method }}"
the code will not reach toin laravel you should use like