Home > Blockchain >  Laravel route destroy return back issue
Laravel route destroy return back issue

Time:12-25

I have a step2 form which will store Order first in step1 then go to step2 page which the purpose is storing Aircon

When Click "trash icon" should go to route destroy but after return back() Error: The GET method is not supported for this route. Supported methods: POST.

How to do correctly? passing $order in destroy ?

public function destroy(Aircon $aircon)
{
    $aircon->delete();
    return back();
}
public function store(Request $request, Order $order)
{
    $order = Order::find($order->id);

    $attributes = $this->validateAirCon();

    $order->aircons()->create($attributes);

    return view('pages.user.order-aircons.addAircon',compact('order'));
}
<form action="{{ route('aircon.store', $order) }}" method="post">
    @csrf

    {{--inputs... --}}
    
    <button type="submit">aircon.store</button>
</form>

@forelse ($order->aircons as $aircon)
    <table >
        <th>Model Number</th>
            {{-- th.. --}}
        <tr>
            {{-- td... --}}
            <td>
                {{-- Delete Aircon --}}
                <form action="{{ route('aircon.destroy', $aircon) }}" method="post">
                    @method('DELETE')
                    @csrf
                    {{-- button submit --}}
                </form>
            </td>
        </tr>
    </table>
@empty
    <h1>no data</h1>
@endforelse

enter image description here

CodePudding user response:

You are using GET method except for DELETE method, You need to use DELETE method to destroy.

show us your blade

you can use return redirect()->back();

Or you can redirect user to the same name route

CodePudding user response:

Use the route name:

public function destroy(Aircon $aircon)
{
    $aircon->delete();

    // Put the route name that you want to be redirected to that.
    return redirect()->route('YOUR_ROUTE_NAME');
}

CodePudding user response:

Problem solved

Use custom route to pass two parameters Aircon and Order

Route::delete('/aircon/delete/{aircon}/order{order}', [AirConController::class, 'delete'])->name('aircon.destroy');
public function delete(Aircon $aircon, Order $order)
{
    $aircon->delete();
    return view('pages.user.order-aircons.addAircon', compact('order'));
}
  • Related