PHP code example of yousef-aman / prevent-deletion

1. Go to this page and download the library: Download yousef-aman/prevent-deletion library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yousef-aman / prevent-deletion example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use YousefAman\PreventDeletion\Traits\PreventDeletionIfHasRelations;

class User extends Model
{
    use PreventDeletionIfHasRelations;

    // Exclude 'comments' from the check
    protected $excludedRelations = ['comments'];

    public function blogs()
    {
        return $this->hasMany(Blog::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function likes()
    {
        return $this->hasMany(Like::class);
    }

    // Define specific conditions dynamically
    public function specificConditions(): array
    {
        return [
            [
                'condition' => $this->is_active === false,
                'message' => 'Cannot delete this user because it is not active.'
            ],
            [
                'condition' => $this->is_verified === false,
                'message' => 'Cannot delete this user because it is not verified.'
            ]
        ];
    }

    // Optional: Customize deletion message
    public function getDeletionMessage($default)
    {
        return 'Custom deletion message: ' . $default;
    }
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use YousefAman\PreventDeletion\Traits\PreventDeletionIfHasRelations;

class Blog extends Model
{
    use PreventDeletionIfHasRelations;

    // No exclusions in this example
    protected $excludedRelations = [];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    // Define specific conditions dynamically
    public function specificConditions(): array
    {
        return [
            [
                'condition' => $this->is_published === false,
                'message' => 'Cannot delete this blog because it is not published.'
            ]
        ];
    }

    // Optional: Customize deletion message
    public function getDeletionMessage($default)
    {
        return 'Custom deletion message: ' . $default;
    }
}

namespace App\Http\Controllers;

use App\Models\User;
use YousefAman\PreventDeletion\Exceptions\PreventDeletionException;
use Exception;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function destroy($id)
    {
        try {
            $user = User::findOrFail($id);
            $user->delete();
            return redirect()->back()->with('success', 'User deleted successfully.');
        } catch (PreventDeletionException $e) {
            // Handle deletion prevention specifically
            return redirect()->back()->with('error', $e->getMessage());
        } catch (Exception $e) {
            // Handle other exceptions
            return redirect()->back()->with('error', 'An error occurred: ' . $e->getMessage());
        }
    }
}

namespace YousefAman\PreventDeletion\Exceptions;

use Exception;

class PreventDeletionException extends Exception
{
    /**
     * Create a new PreventDeletionException instance.
     *
     * @param  string  $message
     * @param  int  $code
     * @param  \Exception|null  $previous
     * @return void
     */
    public function __construct($message = "", $code = 0, Exception $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }
}

public function getDeletionMessage($default)
{
    return $this->deletionMessage ?? $default;
}