Laravel资源商店方法被重定向到销毁方法

时间:2017-10-22 12:47:43

标签: php laravel blade laravel-eloquent laravel-5.5

我有资源路线

Route::resource('climb-excluded','CexcludedController',['only'=>['store','update','destroy']]);

我的代码是为了保存数据

<div class="col-lg-4">
<form class="form" method="POST" action="{{ route('climb-excluded.store') }}">
{{ csrf_field() }}
<div class="card">
    <div class="card-head style-primary">
        <header>Add item</header>
    </div>
    <div class="card-body floating-label">
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <input type="text" class="form-control" id="name" name="name">
                    <label for="name">Name</label>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-10 col-sm-offset-1">
                <button type="submit"
                        class="btn btn-block btn-success ink-reaction">
                    Add
                </button>
            </div>
        </div>
    </div>
</div>

销毁数据的按钮:

{!!  Form::open( array('route'=>array('climb-excluded.destroy', $excluded->id),
'method'=>'DELETE')) !!}
<button type="submit"
    class="btn ink-reaction btn-floating-action btn-sm btn-danger "
    rel="tooltip"
    title="Delete">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</button
{!! Form::close() !!}

存储方法表单控制器:

    public function store(Request $request)
{
    $this->validate($request,[
        'name' => 'required|max:255'
    ]);

    $excluded = new Cexcluded;
    $excluded -> name = $request->name;
    $excluded->save();
    //redirect to
    Session::flash('success','New item sucessfully added !');
    return back()->withInput(['tabs'=>'second4']);
}

销毁方法表单控制器:

    public function destroy($id)
{
    $trekExcluded = Cexcluded::find($id);
    $trekExcluded->tours()->detach();
    $trekExcluded ->delete();
    Session::flash('success','Item sucessfully deleted !');
    return back()->withInput(['tabs'=>'second4']);
}

我遇到的麻烦/错误是我可以成功地将第一行插入表中。但是当我去第二个时,store方法以某种方式重定向到destroy方法并删除第一个插入的行。虽然我已经清楚地声明了表单的操作属性中的store方法。

仅供参考:两条路线都存在于同一视图/页面中。使用col-md-8循环foreach中的方法,col-md-4

中存储方法

1 个答案:

答案 0 :(得分:0)

很明显,您的表单没有唯一的nameid,这就是为什么第二种方法被重定向到destroy方法的原因。做这样的事情:

cex-store-1
cex-destroy-1