Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.
Delete can be done in several ways in laravel.
Basically there are two types of delete in laravel
In soft delete, you can restore the record using pre defined method in laravel.
To delete a record permanently in laravel you can use folloing code :
First get the instance of the model
$flight=App\\Flight::find(1);
or conditional search
$flight=App\\Flight::where('is_active',true)->first();
then call the delete method
$flight->delete();
If you know the id (primary key) of the record, you can use destroy method directly.
1. Delete one record
ModelName::destroy(id);
i.e App\\Flight::destroy(1)
2. Delete Multiple record at once
ModelName::destroy([id1, id2]);
i.e App\\Flight::destroy([1,2,3]);
or
ModelName::destroy(id1, id2);
i.e App\\Flight::destroy(1,2,3);
The above is identical to this:
DB::table('users')->where('id', $id)->delete();If you wish to truncate the entire table, which will remove all rows and reset the auto-incrementing ID to zero, you may use the truncate method:
DB::table('users')->truncate();
DB::table('xxx')->where('id', '>', 100)->delete();
If this is a record from database,
App\\ModelName::destroy($id);
You can use soft deletes for hiding from view,edit,delete.
Use destory function in laravel to delete a record.
If you are using Elouqent.
just
get specific opject using "id or name what ever" then
.delete();
Ex::
$user=User::find(UserId);
$user->delete();
-------------------------------------------------
we have the request life cycle position to use delete statment in laravel - routesRoute::delete();
- eg. \\app\\Http\\Controllers\\PagesController.phppublic function destroy($id){ use \\App\\Page; Page::destroy($id); return redirect()->route('pages.index')->with('alert' , 'Page Deleted Successfuly!');}
- view
<form action="{{ route('pages.destroy', $page->id) }}" method="POST" class="pull-left">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button class="btn btn-danger"><i class="fa fa-times"></i></button>
</form>