pass parameter to controller with forms in laravel-9

i want to send $category to controller but when i recive $category in controller i found that it is empty

my form in edit.blade.php

    <form action="{{ route('categoryUpdate', $category) }}" method="post" class="ui form">
        @method('put')
        @csrf
        <div class="field">
            <label for="title">title</label>
            <input type="text" name="title" value="{{$category->title}}" />  
        </div>
        <div class="field">
            <label for="description">description</label>
            <textarea name="description">{{$category->description}}</textarea>
           
        </div>
        <div class="field">
            <label for="active">status</label>
            <select name="active">
                <option value="0" @if($category->active==0) selected @endif>Not Active</option>
                <option value="1" @if($category->active==1) selected @endif> Active</option>
            </select>
        </div>
        <div class="field">
            <button type="submit" class="ui primary button"> edit</button>
        </div>
    </form>

my web.php

Route::get('/category/{category}',[CategoryController::class,'show'])->name('categoryShow');
Route::get('/category/edit/{category}',[CategoryController::class,'edit'])->name('categoryEdit');

my controller

public function update(UpdateCategoryRequest $request, Category $category)
    {
        dd($category);
    }

Upvotes: 0

Views: 1805

Answers (1)

Serghei Leonenco
Serghei Leonenco

Reputation: 3507

In order to get the updates you need to do this: Changes in the form:

<form action="{{ route('categoryUpdate', $category) }}" method="post" class="ui form">
        @csrf
        <div class="field">
            <label for="title">title</label>
            <input type="text" name="title" value="{{$category->title}}" />  
        </div>
        <div class="field">
            <label for="description">description</label>
            <textarea name="description">{{$category->description}}</textarea>
           
        </div>
        <div class="field">
            <label for="active">status</label>
            <select name="active">
                <option value="0" @if($category->active==0) selected @endif>Not Active</option>
                <option value="1" @if($category->active==1) selected @endif> Active</option>
            </select>
        </div>
        <div class="field">
            <button type="submit" class="ui primary button"> edit</button>
        </div>
    </form>

Routes:

Route::post('/category/{category}',[CategoryController::class,'store'])->name('categoryStore');
Route::post('/category/edit/{category}',[CategoryController::class,'update'])->name('categoryUpdate');

When we deal with API we use PUT to update the entity, but when you deal with web form you use a POST method and that is most popular way to update the entity.

Upvotes: 1

Related Questions