Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda