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);
return redirect()->route('admin.flags');
}