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 class="m-auto w-4/8 py-24">
<div class="text-center">
<h1 class="text-5xl uppercase bold">
Update Car
</h1>
</div>
</div>
<div class="flex justify-center pt-20">
<form action="../cars/{{ $car->id }}" method="POST">
@csrf
@method('PUT')
<div class="block">
<input type="text" class="shadow-5xl mb-10 p-2 w-80 italic placeholder-gray-400" name="name"
value="{{ $car->name }}"><br>
<input type="number" class="shadow-5xl mb-10 p-2 w-80 italic placeholder-gray-400" name="founded"
value="{{ $car->founded }}"><br>
<input type="text" class="shadow-5xl mb-10 p-2 w-80 italic placeholder-gray-400" name="description"
value="{{ $car->description }}"><br>
<button type="submit" class="bg-teal-500 block shadow-5xl mb-10 p-2 w-80 uppercase font-bold text-white">
Update
</button>
</div>
</form>
</div>
@endsection
the last part contains the buttons for delete and edit
@foreach ($cars as $car )
<div class="m-auto">
<span class="uppercase text-teal-500 font-bold text-xs italic">
Founded : {{ $car->founded }}
</span>
<h2 class="text-gray-700 text-5xl">
{{ $car->name }}
</h2>
<p class="text-lg text-gray-700 py-6">
Description : {{ $car->description }}
</p>
<div class="float-right">
<a class=" pb-2 italic text-teal-500" href="cars/{{ $car->id }}/edit">
Edit →
</a>
<form action="../cars/{{ $car->id }}" method="POST">
@csrf
@method("delete")
<button type="submit" class="pb-2 italic text-red-500">
Delete →
</button>
</form>
</div><br><br>
<hr class="mt-4 mb-8">
</div>
@endforeach
here is my route
Route::resource('/cars', CarsController::class);
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) { ... }
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
So, Basically when you use resource you get predefined route list by Laravel with different methods.
Example your route is
Route::resource('/cars', CarsController::class);
Then laravel generate routes like this. To check route list run
php artisan route:list
Route::GET('/cars', [CarsController::class, 'index'])->name('cars.index);
Route::GET('/cars/create', [CarsController::class, 'create'])->name('cars.create');
Route::POST('/cars', [CarsController::class, 'store'])->name('cars.store');
Route::GET('/cars/{id}', [CarsController::class, 'show'])->name('cars.show');
Route::GET('/cars/{id}/edit', [CarsController::class, 'edit'])->name('cars.edit');
Route::PUT('/cars/{id}', [CarsController::class, 'update'])->name('cars.update');
Route::DELETE('/cars/{id}', [CarsController::class, 'destroy'])->name('cars.destroy');
Then you can use in form with defined methods.
Example to use
{{ route('cars.destroy', ['id' => $car->id]) }}
Source Laravel documentation: click to check more resource method on offical Laravel website.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.