How can I create a destroy function to delete from the database, and if it is saved in the database, the color of the button changes to red, otherwise green. My form stores data correctly, but I can not destroy it.
Blade
@foreach (range(1, 3) as $item)
<form method="post" action="/seats">
@csrf
<input type="hidden" name="user_id" value="{{Auth::user()->id}}"/>
<input type="hidden" name="row_seats" value="4"/>
<input type="hidden" name="seat_id" value="{{ $item }}"/>
<button type="submit" style="width:60px; margin-left:10px;"
class="btn btn-success">
{{$item}}
</button>
</form>
<form method="POST" action="/seats/{{ $seats->id }}">
<button type="submit" style="margin-left:10px;" class="btn btn-danger">
Unreserve {{$item}}
</button>
</form>
@endforeach
Destroy function
public function destroy($id)
{
$seats = Seats::find($id);
$seats->delete();
return redirect('/seats');
}
Route
Route::post('/seats/{seats}',[App\Http\Controllers\SeatsController::class, 'destroy'])
->name('seats.destroy');
My destroy function already work.Now i am trying if it's already saved in database it is showing only red button for delete.
@foreach (range(1, 3) as $item)
@if($seat_id > 0 )
<form method="post" action="{{ route('seats.destroy', $item) }}">
@csrf
@method('delete')
<button type="submit" style="margin-left:10px;" class="btn btn-danger">Unreserve {{$item}}</button>
</form>
@else
<form method="post" action="/seats">
@csrf
<input type="hidden" name="row_seats" value="4"/>
<input type="hidden" name="seat_id" value="{{ $item }}"/>
<button type="submit" style="width:60px; margin-left:10px;" class="btn btn-success">{{$item}}</button>
</form>
@endif
@endforeach
Index function
$seat_id = $seats->id;
if somebody need to see destroy function how it is created
public function destroy($id)
{
$seats = Seats::find($id);
if ($seats != null) {
$seats->delete();
return redirect('/seats')->with('success', 'Post Removed Succesfully !');
}
return redirect('/seats')->with('status','Something went wrong !');
}
I think you are passing the wrong variable to the destroy route which is the seat id in the foreach loop. change this
action="/seats/{{ $seats->id }}"
to
action="{{ route('seats.destroy', $item) }}"