How to check PHP if record in db is deleted?

I delete the message:

$this->delete('/api/posts/1');

      

How do I check if this deletes?

$this->assertNull(Post::find($post->id)); 

      

This does not work!

+3


source to share


4 answers


try this:

$this->notSeeInDatabase('posts', ['id' => $post->id]);

      

description:

->notSeeInDatabase($table, array $data)  

      



and

->missingFromDatabase($table, array $data)

      

One is just a nickname for the other.

+3


source


Most of the answers are way complex or using an extra function to call. Laraval delete function returns boolean or null

/**
 * Delete the model from the database.
 *
 * @return bool|null
 * @throws \Exception
 */
public function delete()
{
  ....
  ....
}

      



So you can just use this code to check for deletion.

try {
  if ($this->delete('/api/posts/1')) {
  }    
} catch (\Exception $exception) {}

      

+1


source


You can check it with Post::findOrFail()

, or you can just check it out if(Post::findById($id) == null)

.

Edit: Not sure if this still works in Laravel> 5.2, but you can get the collection withTrashed()

if you softly delete the entry. Then you can use if($record->isTrashed()) { ... }

. This function is not used by default in the model, but is present in the characteristic SoftDelete

.

Here you will find information for editing: link .

0


source


You should try the following:

$post = Post::find($post->id); 

if(is_null($post))

      

Hope this job is for you!

0


source







All Articles