laravel withtrashed 1

laravel withtrashed

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

Here is what the above code is Doing:
1. We are using the find method to find the user with the id that is passed in the url.
2. We are using the delete method to delete the user.
3. We are returning a json response with a status of 1 and a message of success if the user is deleted.
4. We are returning a json response with a status of 0 and a message of fail if the user is not deleted.

Now, let’s test the delete method.

Go to the browser and type the following url:
http://localhost:8000/api/users/1

You should see the following output:
{“status”:”1″,”msg”:”success”}

Now, go to the database and check if the user with the id of 1 is deleted.

If you go to the browser and type the following url:
http://localhost:8000/api/users

You should see the following output:
[{“id”:2,”name”:”John”,”email”:”john@example.com”,”created_at”:”2017-05-29 11:00:00″,”updated_at”:”2017-05-29 11:00:00″}]

As you can see, the user with the id of 1 is deleted.

Now, let’s test the delete method if the user is not deleted.

Go to the browser and type the following url:
http://localhost:8000/api/users/3

You should see the following output:
{“status”:”0″,”msg”:”fail”}

Now, go to the database and check if the user with the id of 3 is deleted.

If you go to the browser and type the following url:
http://localhost:8000/api/users

You should see the following output:
[{“id”:1,”name”:”John”,”email”:”john@example.com”,”created_at”:”2017-05-29 11:00:00″,”updated_at”:”2017-05-29 11:00:00″},{“id”:2,”name”:”John”,”email”:”john@example.com”,”created_at”:”2017-05-29 11:00:00″,”updated_at”:”2017-05-29 11:00:00″}]

As you can see, the user with the id of 3 is not deleted.

Now, let’s test the delete method if the user is not found.

Go to the browser and type the following url:
http://localhost:8000/api/users/4

You should see the following output:
{“status”:”0″,”msg”:”fail”}

Now, go to the database and check if the user with the id of 4 is deleted.

If you go to the browser and type the following url:
http://localhost:8000/api/users

You should see the following output:
[{“id”:1,”name”:”John”,”email”:”john@example.com”,”created_at”:”2017-05-29 11:00:00″,”updated_at”:”2017-05-29 11:00:00″},{“id”:2,”name”:”John”,”email”:”john@example.com”,”created_at”:”2017-05-29 11:00:00″,”updated_at”:”2017-05-29 11:00:00″}]

As you can see, the user with the id of 4 is not deleted.

Now, let’s test the delete method if the user id is not passed.

Go to the browser and type the following url:
http://localhost:8000/api/users/

You should see the following output:
{“status”:”0″,”msg”:”fail”}

Now, go to the database and check if the user with the id of 4 is deleted.

Similar Posts