Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel Eloquent ORM delete() method

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?

like image 889
Evol Rof Avatar asked Aug 02 '17 10:08

Evol Rof


People also ask

How do I delete ORM?

It is easy to perform delete operation on a single table. All you have to do is to delete an object of the mapped class from a session and commit the action. However, delete operation on multiple related tables is little tricky.

What does eloquent delete return?

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 .

How do I delete a record in Laravel model?

Step 1: Create Controller UserController by executing this command. Step 2: We can delete records in two ways. Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one). ->name( 'users.


2 Answers

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

$res=User::where('id',$id)->delete(); 
like image 56
AddWeb Solution Pvt Ltd Avatar answered Oct 11 '22 22:10

AddWeb Solution Pvt Ltd


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 will returns true/false

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

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

//assume you have 10 users, id from 1 to 10; $result=User::where('id','<',11)->delete(); //returns 11 (the number of the records had been 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 had been 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 had been 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);

like image 38
Evol Rof Avatar answered Oct 11 '22 21:10

Evol Rof