【问题标题】:Delete method in controller and delete buttons on the browser connection控制器中的删除方法和浏览器连接上的删除按钮
【发布时间】:2022-01-06 13:28:12
【问题描述】:

所以我希望我的屏幕上有一条警告消息,询问我是否要删除库存中的项目,如果他们点击是,它将被删除并重定向到库存列表,但是如果我现在点击否,它会删除,我希望它取消并重定向回主库存页面。我不确定该怎么做。 这是我的控制器中的删除和销毁方法:

/**
     * @param Inventory $inventory
     * @return \Illuminate\Contracts\View\View
     */
    public function delete(Inventory $inventory)
    {
        return view('pages.inventories.delete', ["inventory" => $inventory])
            ->with('warning', 'Do you wish to delete this item?');
        return redirect(destroy);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  Inventory $inventory
     * @return RedirectResponse
     */
    public function destroy(Inventory $inventory): RedirectResponse
    {
        $inventory->delete();
        return redirect()->route('inventories.index')->with('success',
            'Item has been deleted!');
    }

这是我的 delete.blade 文件:

@extends('layouts.app')

@section('title', 'Delete Inventory')

@section('content')
    <h1><strong>Delete inventory</strong></h1>
    {{ $inventory }}
    <form method="POST" action="{{url('/inventories', $inventory)}}">
       @method('delete')
        @csrf
        @if ($message = Session::get('success'))
            <div class="alert alert-success alert-block">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
            </div>
        @endif
        @if ($message = Session::get('warning'))
            <div class="alert alert-warning alert-block">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
            </div>
        @endif
        <h3>Do you wish to delete this item?</h3>
        <button type="yes">Yes</button>
        <button type="no">No</button>
    </form>
@endsection

我的路由器很好并且可以正常工作,但在这里它们以防万一:

Route::get('/inventories/{inventory}/delete', [\App\Http\Controllers\InventoryController::class, 'delete'])->name('inventories.delete');

Route::delete('/inventories/{inventory}', [\App\Http\Controllers\InventoryController::class, 'destroy'])->name('inventories.destroy');

感谢您的帮助!

【问题讨论】:

    标签: php methods destroy


    【解决方案1】:

    在我的 delete.blade 文件中,如果单击“是”并且如果单击“否”,我需要执行此操作以提交我的表单以销毁和库存项目,然后将其重定向到库存页面(主页)。

    @extends('layouts.app')
    
    @section('title', 'Delete Inventory')
    
    @section('content')
        <h1><strong>Delete inventory</strong></h1>
        {{ $inventory }}
        <form method="POST" action="{{url('/inventories', $inventory)}}">
           @method('delete')
            @csrf
            <button type="submit">Yes</button>
                <a href="/inventories">No</a>
        </form>
    @endsection
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多