Home > OS >  How do i get a post id in laravel
How do i get a post id in laravel

Time:08-17

i am new to Laravel so am trying to delete and edit some posts which is linked to a page where the update from is located but each time i update or delete, i get a 404 error or the page is not found(i think the problem is the url).

here is my code for the update

public function update(Request $request, $id)   {
$car = Car::where('id', $id)
     ->update([
        'name'=> $request->input('name'),
        'founded'=> $request->input('founded'),
        'description' => $request->input('description')
]);

return redirect('/cars'); }

this one is for delete/destroy

public function destroy($id)
{
    $car = Car::find($id);
    $car->delete();
    return redirect('/cars');
}

i also have an edit.blade.php

@section('content')
<div >
    <div >
        <h1 >
            Update Car
        </h1>
    </div>
</div>
<div >
    <form action="../cars/{{ $car->id }}" method="POST">
        @csrf
        @method('PUT')
        <div >
            <input type="text"  name="name" 
            value="{{ $car->name }}"><br>

            <input type="number"  name="founded" 
            value="{{ $car->founded }}"><br>

            <input type="text"  name="description" 
            value="{{ $car->description }}"><br>

            <button type="submit" >
              Update
            </button>
            
        </div>
    </form>
</div>

@endsection

the last part contains the buttons for delete and edit

 @foreach ($cars as $car )
            <div >
                
                <span >
                    Founded : {{ $car->founded  }}
                </span>
                <h2 >
                  {{ $car->name }}
                </h2>
                <p >
                    Description : {{ $car->description }} 
                 </p>
                 <div >
                    <a  href="cars/{{ $car->id }}/edit">
                      Edit &rarr;
                    </a>

                    <form action="../cars/{{ $car->id }}" method="POST">
                     @csrf
                     @method("delete")
                    <button type="submit" >
                        Delete &rarr;
                    </button>
                    </form>
                  </div><br><br>

            <hr >
            </div>
       @endforeach

here is my route

Route::resource('/cars', CarsController::class);

CodePudding user response:

first check route with this command php artisan route:list then you see list like this

DELETE          cars/{car}
PUT|PATCH       cars/{car}

the car name is important to automatically Laravel find entity base on Type hint Car $car, so in controller use this convention :

public function destroy(Car $car)
{
    $car->delete();
    return redirect('/cars');
}
public function update(Request $request, Car $car) { ... } 

CodePudding user response:

You should not generate url like this: action="../cars/{{ $car->id }}"

Instead use action="{{ route('cars.update', $car->id) }}"

You can see the available routes by running this command php artisan route:list

CodePudding user response:

You need to specify a route in routes.php

Route::post('/cars/{id}', ......);

Then in the controller and function you are calling with that route the {id} will be a parameter

public function myControllerRoute($id, Request $request) {

}

You can also instead pass the ID as a hidden input field and retrieve it on /cars route with the $request

  • Related