79

Hi I am studying laravel. I use Eloquent ORM delete method but I get a different result.Not true or false but null. I set an resource route and there is a destroy method in UsersController.

public function destroy($id){

  $res=User::find($id)->delete();
  if ($res){
    $data=[
    'status'=>'1',
    'msg'=>'success'
  ];
  }else{
    $data=[
    'status'=>'0',
    'msg'=>'fail'
  ];
  return response()->json($data);

But I always get a response {"status":"0","msg":"failed"},the record in the database is deleted.

Then I use dd($res) .It shows null in the page.

But from the course I learn it returns a boolean value true or false.

Is there any error in my code?

Can you tell me some other method that I can get a boolean result when I delete data from database?

Evol Rof
  • 2,528
  • 2
  • 22
  • 37

7 Answers7

136

I think you can change your query and try it like :

$res=User::where('id',$id)->delete();
Mel
  • 5,837
  • 10
  • 37
  • 42
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
47

Before delete , there are several methods in laravel.

User::find(1) and User::first() return an instance.

User::where('id',1)->get and User::all() return a collection of instance.

call delete on an model instance returns true/false

$user=User::find(1);
$user->delete(); //returns true/false

call delete on a collection of instance returns a number which represents the number of the records deleted

//assume you have 10 users, id from 1 to 10;
$result=User::where('id','<',11)->delete(); //returns 11 (the number of the records deleted)

//lets call delete again
$result2=User::where('id','<',11)->delete(); //returns 0 (we have already delete the id<11 users, so this time we delete nothing, the result should be the number of the records deleted(0)  ) 

Also there are other delete methods, you can call destroy as a model static method like below

$result=User::destroy(1,2,3);
$result=User::destroy([1,2,3]);
$result=User::destroy(collect([1, 2, 3]));
//these 3 statement do the same thing, delete id =1,2,3 users, returns the number of the records deleted

One more thing ,if you are new to laravel ,you can use php artisan tinker to see the result, which is more efficient and then dd($result) , print_r($result);

ahmad bd
  • 53
  • 6
Evol Rof
  • 2,528
  • 2
  • 22
  • 37
19

At first,

You should know that destroy() is correct method for removing an entity directly via object or model and delete() can only be called in query builder.

In your case, You have not checked if record exists in database or not. Record can only be deleted if exists.

So, You can do it like follows.

$user = User::find($id);
    if($user){
        $destroy = User::destroy(2);
    }

The value or $destroy above will be 0 or 1 on fail or success respectively. So, you can alter the $data array like:

if ($destroy){

    $data=[
        'status'=>'1',
        'msg'=>'success'
    ];

}else{

    $data=[
        'status'=>'0',
        'msg'=>'fail'
    ];

}

Hope, you understand.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
  • thanks for the note.I use the query builder (where) and it works.Then I try User::destroy($id) it returns 0.But the record in the table disappear .I have a foreign key on id,Is that the reason that I get a zero result while the deletion works? – Evol Rof Aug 02 '17 at 11:58
4

Laravel Eloquent provides destroy() function in which returns boolean value. So if a record exists on the database and deleted you'll get true otherwise false.

Here's an example using Laravel Tinker shell.

delete operation result

In this case, your code should look like this:

public function destroy($id)
    {
        $res = User::destroy($id);
        if ($res) {
            return response()->json([
                'status' => 1,
                'msg' => 'success'
            ]);
        } else {
            return response()->json([
                'status' => 0,
                'msg' => 'fail'
            ]);
        }
    }

More info about Laravel Eloquent Deleting Models

Abdelsalam Shahlol
  • 1,621
  • 1
  • 20
  • 31
4
$model=User::where('id',$id)->delete();
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Ahmed Amir
  • 124
  • 6
2

check before delete the user otherwise throws error exeption

$user=User::find($request->id);
  
   if($user)
   {
   // return $user;           <------------------------user exist
       if($user->delete()){
         return 'user deleted';
         }
    else{
       return "something wrong";
        }   

   }
  else{
     return "user not exist";// <--------------------user not exist
    }
Balaji
  • 9,657
  • 5
  • 47
  • 47
2

You can delete the data using two ways. First, using find Second, using where with model You can try something like this. First:

User::find($id)->destroy();

Or Second Way

User::where('id', $id)->delete();

Now Please try it will return you True/False result.

M Umer Yasin
  • 284
  • 3
  • 6