Delete
The delete method removes a single record that matches
the provided unique filter. It can optionally return the deleted record using select or include.
Purpose
The delete method is used to delete a specific record identified by a unique field. It ensures the record exists before deletion and can return its data after being deleted. This avoids ambiguous deletions and maintains data consistency.
Parameters
- where — Required. A unique filter identifying the record to delete.
- select — Optional. Specifies which fields of the deleted record to return.
- include — Optional. Includes related models when returning the deleted record.
Return Value
Returns an associative array containing the deleted record data.
If the record does not exist or the criteria is not unique, an error-style array is returned:
['modelName' => 'User', 'cause' => 'Record not found'].
Error Handling
- Throws an Exception if
whereis missing or not an associative array. - Throws an Exception if
selectandincludeare used together. - Throws an Exception on any database or transaction error.
Example Usage
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$deletedUser = $prisma->user->delete([
'where' => ['id' => 'someUserId'],
]);
echo "<pre>";
echo "Deleted User: " . print_r($deletedUser, true);
echo "</pre>";
Example Using include
$deletedUser = $prisma->user->delete([
'where' => ['id' => 'someUserId'],
'include' => ['posts' => true]
]);
echo "<pre>";
echo "Deleted User: " . print_r($deletedUser, true);
echo "</pre>";
Example Using select
$deletedUser = $prisma->user->delete([
'where' => ['id' => 'someUserId'],
'select' => [
'name' => true,
'email' => true,
'posts' => true
]
]);
echo "<pre>";
echo "Deleted User: " . print_r($deletedUser, true);
echo "</pre>";