Delete records from Database: remove extra data or records from the MySQL table using delete query in Laravel 5. Follow the simple steps and delete all records to match ID, which ID value define in the button.
You need to crate a delete button and define the id value to select ID value from the database and set a onClick function and generate a function name. Put the ID value in the function name. That’s like:
Delete Button:
<button type="buton" class="btn" onclick="deldata({{$val->id}})">Delete</button>
Pass the ID value in the other URL link using JavaScript and define URL link. For example:
Script:
<script type="text/javascript"> function deldata(id){ bootbox.confirm({ size: "small", message: "Are you sure?", callback: function(result){ if(result==true){ window.location.href="{{URL::to('delete_records')}}/"+id; } } }); } </script>
Go to the route page and generate a controller function name and pass the id value. Using this code:
Routes.php:
Route::get('delete_records/{id}','DeleteController@delete_records');
I am create a Delete controller and give the controller class name DeleteController. Now access the Model class and generate the model function name with set the ID value. That is:
Controller:
<?php public static function delete_records($id){ $data=with(new DeleteModel)->deleteRecords($id); if($data){ return redirect('page URL'); }else{ echo "Delete Error!!"; } } ?>
Remove all records from the MySQL table using delete query in the model page. In the model page use where clause for delete records from the database. Follow the code:
Model:
<?php public function deleteRecords($id){ $data=DB::table('Table Name')->where('id',$id)->delete(); if($data){ return true; }else{ return false; } } ?>