Reputation: 445
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');
}
Upvotes: 6
Views: 21079
Reputation: 3460
Try this:
Controller:
public function destroy(Report $report){
$report->delete();
return redirect()->route('admin.flags');
}
Upvotes: 2
Reputation: 21681
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!
Upvotes: 5
Reputation: 1119
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
Upvotes: 1
Reputation: 163748
You're creating get
link but using post
route. Change it to:
Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');
Upvotes: 1