简体   繁体   中英

Delete row from database table with Laravel 5.2

Recently I've started with Laravel 5.2 and I'm trying to make delete button which will delete row from database. Very basic and trivial but seems I can't make it.

I'm following documentation for delete: https://laravel.com/docs/5.2/queries#deletes

And I have made this. My route:

Route::post('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

Button in the view

{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}

and the controller

public function destroy(Request $request){

    $report = $request['report_id'];      

    Report::find($report);

    $report->delete();        
    $request->session()->flash('alert-success', ' Report is deleted successfully.');

    return redirect()->route('admin.flags');
}

I've tried solutions from other threads but I always got error:

MethodNotAllowedHttpException in compiled.php line 8936:

New error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reports.id' in 'where clause' (SQL: select * from `reports` where `reports`.`id` is null limit 1

Why is searching for id instead of report_id ?

UPDATE:

button

{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}

Controller

public function destroy(Request $request){

    $report = $request['report_id'];      

    dd( $request->input('delete'));

    Report::where('report_id', $report)->first();

    $report->delete();        
    $request->session()->flash('alert-success', ' Report is deleted successfully.');

    return redirect()->route('admin.flags');
}

Route

Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

Update 2: This seems to work but is it secure enough? view:

 {!! Form::open(array('route' => array('admin.flags.destroy', $flag->report_id), 'method' => 'get')) !!}
        <button type="submit">Delete</button>
 {!! Form::close() !!}</td> 

Controller

public function destroy($report_id){

  Report::destroy($report_id);
  //$request->session()->flash('alert-success', ' Report is deleted successfully.');

  return redirect()->route('admin.flags');
}

I think your code need to update like:

public function destroy($delete){

   $report = $delete;      

   $rsltDelRec = Report::find($report);

   $rsltDelRec->delete();        
   $request->session()->flash('alert-success', ' Report is deleted successfully.');

   return redirect()->route('admin.flags');
}

Hope this work for you!

You're creating get link but using post route. Change it to:

Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

MethodNotAllowedHttpException means that you are trying to access route with bad method. If you use Html::linkRoute then anchor is generated, but in your routes you have defined Route::post . You need to replace Route::post with Route::get . But if you want to make it safer you need to create simple form with delete button and CSRF token.

<form method="POST" action="{{ URL::route('admin.flags.destroy', {'delete' => $flag->report_id}) }}">
    {{ csrf_field() }}
    <!-- submit button -->
</form>

Why is searching for id instead of report_id?

You need to replace

Report::find($report);

with

$report = Report::where('report_id', $report)->first();

public function destroy(Request $request){

    $report = $request['report_id'];
    ....

You are trying here to access report_id in request, but in routes you named your parameter as delete

Try this:

Controller:

  public function destroy(Report $report){

          $report->delete();

          return redirect()->route('admin.flags');

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM