0
votes

I have a resource route

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

And my code in view to save data

<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>

A button to destroy data:

{!!  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() !!}

Store method form controller:

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

Destroy method form controller:

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

The trouble/bug that I'm facing is I can insert first row into table successfully. But when I go for the second one, the store method is somehow redirected to destroy method and deletes the first inserted row also. While I've clearly declared store method in action attribute of the form.

FYI: Both routes exists in same view/page. Destroy method in col-md-8with foreach loop while store method in col-md-4

1

1 Answers

0
votes

Its quite obvious, that your form don't have a unique name or id, so that's why the second method is redirected to destroy method. Do something like this:

cex-store-1
cex-destroy-1