Skip to content

Soft Deletes

Gilberto Junior edited this page Dec 5, 2019 · 11 revisions

Avaliable for classes that extends CodeHappy\DataLayer\Repository.

Usage

<?php

namespace App\Repositories;

use CodeHappy\DataLayer\Repository;
use CodeHappy\DataLayer\Traits\SoftDeletes;

class UserRepository extends Repository
{
    use SoftDeletes;
}

Methods

The same found in the Laravel Documentation.

With Trashed

Retrieve all records, including records from the trash.

Syntax

public function withTrashed(): self;

Example

$allRecords = $repository
    ->withTrashed()
    ->fetch();

Only Trashed

Retrieve only the records from trash.

Syntax

public function onlyTrashed(): self;

Example

$allRecords = $repository
    ->onlyTrashed()
    ->where('id < 1000')
    ->fetch();

Restore From Trash

Restore the trashed records.

Syntax

public function restoreFromTrash(): bool|null

Example

$restored = $repository
    ->onlyTrashed()
    ->restoreFromTrash();

if ($restored === null) {
    echo 'No registers found';
} elseif ($restored === true) {
    echo 'Registers recovered';
}

🡄 Cacheable | Exit 🡆