user16466807
user16466807

Reputation:

Laravel route destroy return back issue

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 class="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

Upvotes: 0

Views: 182

Answers (2)

user16466807
user16466807

Reputation:

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'));
}

Upvotes: 0

baleghsefat
baleghsefat

Reputation: 302

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');
}

Upvotes: 1

Related Questions