ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How to use delete Statement in Laravel?

user-image
تم إضافة السؤال من قبل Julfkar Moh Umar , Sr. Software Engineer , Aakash Edutech Private Limited
تاريخ النشر: 2017/09/28
Rashedul Hoque
من قبل Rashedul Hoque , Sr. Software Developer , Orko Health Ltd.

Delete can be done in several ways in laravel.

Basically there are two types of delete in laravel

 

  1. soft delete
  2. permanent delete

 

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

Md Musfiqur Rahman Firoz
من قبل Md Musfiqur Rahman Firoz , Senior Programmer , Computer Network Systems Ltd (CNSBD)

DB::table('users')->delete($id);

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();

Partha Sarathi Basu Baul
من قبل Partha Sarathi Basu Baul , Project Leader , Nest Innovative Soluions Pvt. Ltd.

DB::table('xxx')->where('id', '>', 100)->delete();

Muhammad Hamza
من قبل Muhammad Hamza , Web Developer

DB::table('users')->where('id', $id)->delete();

Jayalakshmi Ramasamy
من قبل Jayalakshmi Ramasamy , Web Developer

  1. To delete a record from DB, $model->delete(); // The record is deleted from DB permanently if you do not use SofeDelete option in Model. 
  2. To delete the session, $this->session->flush();
  3. We can use "DELETE" action in form to call destroy() of resource controller.

Lanka Senanayaka
من قبل Lanka Senanayaka , Senior Software Engineer , Ensiz former Fullstack Labs

If this is a record from database,

App\\ModelName::destroy($id);

 

You can use soft deletes for hiding from view,edit,delete.

Saif  Ullah
من قبل Saif Ullah , Software Engineer , NetSol Technology Inc

Use destory function in laravel to delete a record.

If you are using Elouqent.

Adel Abou Elezz
من قبل Adel Abou Elezz , Full Stack Developer , Rama Group

just 

get specific opject using "id or name what ever" then 

.delete();

Ex::

$user=User::find(UserId);

$user->delete();

-------------------------------------------------

mostafa Salah
من قبل mostafa Salah , Senior Web Developer , SEO Egypt

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>

 

المزيد من الأسئلة المماثلة