Delete row from database table with Laravel 5.2

I recently started with Laravel 5.2, and I'm trying to make a delete button that will delete a row from the database. Very simple and trivial, but it seems I can not do this.

I follow the documentation for removal: https://laravel.com/docs/5.2/queries#deletes

And I did it. My route:

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

Button in view

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

and 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 tried solutions from other threads, but always got an error:

The NotAllowedHttpException method in compiled.php 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 a search performed idinstead 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 safe enough 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');
}
+4
4

, , :

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

, !

+4

get, post. :

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

MethodNotAllowedHttpException , . Html::linkRoute, , Route::post. Route::post Route::get. , CSRF.

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

report_id?

Report::find($report);

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

public function destroy(Request $request){

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

report_id , delete

+1

:

:

  public function destroy(Report $report){

          $report->delete();

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

    }
+1

Source: https://habr.com/ru/post/1667111/


All Articles