mardi 27 mars 2018

Laravel destroy and update methods not working

Trying to delete entries using the destroy method in Laravel controller.

public function destroy($id)
{
    $university = University::find($id);
    $university->delete();
    return redirect('/universities');
}

And this is what i'm using in the view

            {!!Form::open(['action' => ['UniversityController@destroy', $university->Id], 'method' => 'POST'])!!}
            
                
            {!!Form::close()!!}

Getting no errors and browser redirects after the button is activated as instructed, but the entry still remains in the veiw list and in the DB. Using MySQL.

Posting to the DB also works fine, but having same problems with update method. No errors and get redirected as I should but no update has happened.

public function update(Request $request, $id)
{
    $this->validate($request, [
        'Name' => 'required',
        'Country' => 'required'
    ]);

    $university = University::find($id);
    $university->Name = $request->input('Name');
    $university->Country = $request->input('Country');
    $university->save();

    return redirect('/universities');
}

{!! Form::open(['action' => ['UniversityController@update', $university->Id], 'method' => 'POST']) !!}
    <div class="form-group">
        
        
    </div>
    <div class="form-group">
            
            
        </div>
        
    
{!! Form::close() !!}

Also tried running without the hidden form methods, but same result.

My routes:

Route::get('/universities', 'UniversityController@index');

Route::get('/universities/create', 'UniversityController@create');

Route::get('/universities/{id}/edit', 'UniversityController@edit');

Route::put('/universities/{id}', 'UniversityController@update');

Route::post('/universities/create', 'UniversityController@store');

Route::delete('/universities/{id}', 'UniversityController@destroy');



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/DPxWN6
via IFTTT

Aucun commentaire:

Enregistrer un commentaire