Home > Software engineering >  CRUD laravel edit page 404
CRUD laravel edit page 404

Time:12-02

i am trying to edit my categories via crud edit, I think I did everything right both in the controller and in the routes but I keep getting error 404 page not found, as soon as I click on the button to change the category

this is my controller

<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $category=Category::all();
        return view('categorie.categorie', compact('category'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('categorie.crea');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $req)
    {
        
        $category = Category::create([
            'nome'=>$req->nome
        ]);
        return redirect()->back()->with('message', 'Categoria inserita correttamente');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category,$id)
    {
        $category=Category::findOrFail($id);
        dd($category);
        return view('categorie.editcat', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        $category=Category::updated([
            'nome'=>$request->nome,
        ]);
        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category)
    {
        //
    }
}

this is route

Route::get('/categorie',[CategoryController::class,'index'])->name('categorie.categorie');
Route::get('/categorie/create',[CategoryController::class,'create'])->name('categorie.crea');
Route::post('/categorie/store',[CategoryController::class,'store'])->name('categorie.store');
Route::get('/categorie/store/{$id}',[CategoryController::class,'edit'])->name('categorie.edit');
Route::post('/categorie/update/{$id}',[CategoryController::class,'update'])->name('categorie.update');

and this is blade with form for edit

<x-layout>

<div class="container text-center">
    <div class="row justify-content-center">
        <div class="col-12 col-md-12 justify-content-center">
            <h1>Categorie</h1>
            <table class="table">
                <thead>
                  <tr>
                    <th scope="col">ID</th>
                    <th scope="col">NOME</th>
                    <th scope="col">STATO</th>
                    <th scope="col">AZIONI</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach ($category as $cat)
                  <tr>
                    <th scope="row">{{$cat->id}}</th>
                    <th>{{$cat->nome}}</th>
                    <th>--</th>
                    <th><a href="{{route('categorie.crea')}}"><i class="fas fa-plus-circle"></i></a> 
                      <form action="{{route('categorie.edit',$cat->id)}}" method="GET">
                      <button type="submit"><i hljs-string">"></i></button>
                    </form></th>
                  </tr>
                  @endforeach
                </tbody>
              </table>
        </div>
    </div>
</div>

</x-layout>

any solution?

CodePudding user response:

you have to change this uri '/categorie/store/{$id}' to '/categorie/store/{id}' and do that in update route

  • Related