Abdul Raheem Ghani
Abdul Raheem Ghani

Reputation: 336

Link Button Not Working in Laravel

I want to edit a record in a CRUD application in laravel where I have a button which has been linked to go to the index view but when I click it, it redirects me to the UPDATE method of the controller.
This is my form:

{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}

                    <div class="row col-md-10 col-md-offset-1 panel">
                        <div class="col-md-8 col-md-offset-2">

                        <br />

                          <div class="form-group">
                            {{ Form::label('name', 'Player Name') }}
                            {{ Form::text('name', $player->name, ['class' => 'form-control']) }}
                          </div>

                          <div class="form-group">
                                {{ Form::label('file', 'Upload Image') }}
                                {{ Form::file('pic') }}

                            </div>

                            <div class="form-group">

                              {{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
                              {!! Form::close() !!}

                              <a href="{{ route('players.index') }}">
                                <button class="btn btn-danger" >Cancel</button>
                              </a>

                            </div>
                        </div>
                    </div>   

I have the following button for going back to the index page but this is taking me to the UPDATE method of the controller:

<a href="{{ route('players.index') }}">
    <button class="btn btn-danger" >Cancel</button>
</a>  

This is my index method in the controller:

public function index()
{
    $players = Player::paginate(5);
    return view('players.index', compact('players'));
}  

This is the UPDATE method in the controller:

public function update(Request $request, $id)
{
    return "Hi";
}    

This is my route file contents:

Route::resource('news', 'NewsController');

Route::resource('competition', 'CompetitionsController');

Route::resource('players', 'PlayersConroller');

Everything looks fine to me but I don't know what goes wrong here.
Any help is appreciated in advance.

Upvotes: 2

Views: 2497

Answers (1)

Amandeep Singh
Amandeep Singh

Reputation: 274

I am not sure if it will solve your issue, try to put your button code outside the form-group div.

You can change your code as

<a href="{{ route('players.index') }}" class="btn btn-danger">Cancel</a>

You can check your html you have put button inside form tag which of type submit that's why it is submitting the form again.

Replace your form code with:

<div class="row col-md-10 col-md-offset-1 panel">
    <div class="col-md-8 col-md-offset-2">

        {!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
        <br />

        <div class="form-group">
            {{ Form::label('name', 'Player Name') }} {{ Form::text('name', $player->name, ['class' => 'form-control']) }}
        </div>

        <div class="form-group">
            {{ Form::label('file', 'Upload Image') }} {{ Form::file('pic') }}

        </div>

        <div class="form-group">

            {{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}

        </div>
    {!! Form::close() !!}
    </div>
    <a href="{{ route('players.index') }}">
        <button class="btn btn-danger">Cancel</button>
    </a>

</div>

Upvotes: 1

Related Questions