Search code examples
phplaravelcontrollerlaravel-routinglaravel-api

I got new function for API Controller and it's not working at this


I coded update status for category. I got a new function in an API Controller, so when I click submit it is not working.

This is solved when I move to category.update, but I can't because that function is used for something else.

web.php

Route::patch('category/{$category}', 'Admin\CategoryController@change')
    ->name('category.change');
Route::resource('category', 'Admin\CategoryController')
    ->middleware('loggedin');

This is the new function for API Controller:

public function change($category, Request $request)
{
    $cate = Category::find($category);
    if ($cate->category_status == 0) {
        $cate->category_status = 1;
        $cate->save();
    } else {
        $cate->category_status = 0;
        $cate->save();
    }

    return back()->with('success', 'Success!');
}

list.blade.php

<form autocomplete="off" action="{{ route('category.change', [$cate->category_id]) }}" method="POST" enctype="multipart/form-data">
    @method('PATCH')
    @csrf
    <button class="fa fa-eye" type="submit"></button>
</form>

Solution

  • First, in route, delete $

    Route::patch('category/{category}', 'Admin\CategoryController@change')
       ->name('category.change');
    

    Second - use route with named param

    route('category.change', ['category' => $cate->category_id])
    

    Third - in controller action Request must be first

    public function change(Request $request, $category){}