Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
How can I create a destroy function to delete from database and if its saved change color of button

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');
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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 !');
       
    }
about 4 years ago · Juan Pablo Isaza Report

0

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) }}"

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!