Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: cesargb/laravel-cascade-delete
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.1.0
Choose a base ref
...
head repository: cesargb/laravel-cascade-delete
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.github export-ignore
/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
phpstan.neon.dist export-ignore
phpunit.xml.dist export-ignore
.php-cs-fixer.dist.php export-ignore
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
38 changes: 38 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: phpstan

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
phpstan:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-8.2-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-8.2-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: |
composer install
composer dump
- name: Run analyse phpstan
run: vendor/bin/phpstan analyse --error-format github
41 changes: 41 additions & 0 deletions .github/workflows/style-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: style-fix

on:
push:
branches: [master]

jobs:
style:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
coverage: xdebug

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-8.2-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php--8.2
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: |
composer install
composer dump
- name: Fix styles
run: vendor/bin/php-cs-fixer fix
- uses: EndBug/add-and-commit@v9

- name: Run style
run: vendor/bin/php-cs-fixer fix --dry-run --diff --format junit
49 changes: 49 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: tests

on:
- push
- pull_request

jobs:
run-tests:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [8.4, 8.3, 8.2]
laravel: ['10.*', '11.*', '12.*']
dependency-version: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
- laravel: 12.*
testbench: 10.*
exclude:
- laravel: 10.*
php: 8.4

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pdo, sqlite, pdo_sqlite
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
env:
XDEBUG_MODE: coverage
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/vendor
/build
composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
.phpunit.cache
41 changes: 41 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__.'/src',
__DIR__.'/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
'php_unit_method_casing' => ['case' => 'snake_case'],
])
->setFinder($finder);
1 change: 0 additions & 1 deletion .styleci.yml

This file was deleted.

24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

55 changes: 48 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[![Build Status](https://travis-ci.org/cesargb/laravel-cascade-delete.svg?branch=master)](https://travis-ci.org/cesargb/laravel-cascade-delete)
[![StyleCI](https://github.styleci.io/repos/144183283/shield?branch=master)](https://github.styleci.io/repos/144183283)
[![tests](https://github.com/cesargb/laravel-cascade-delete/actions/workflows/tests.yml/badge.svg)](https://github.com/cesargb/laravel-cascade-delete/actions/workflows/tests.yml)
[![phpstan](https://github.com/cesargb/laravel-cascade-delete/actions/workflows/phpstan.yml/badge.svg)](https://github.com/cesargb/laravel-cascade-delete/actions/workflows/phpstan.yml)
[![style-fix](https://github.com/cesargb/laravel-cascade-delete/actions/workflows/style-fix.yml/badge.svg)](https://github.com/cesargb/laravel-cascade-delete/actions/workflows/style-fix.yml)
[![Quality Score](https://img.shields.io/scrutinizer/g/cesargb/laravel-cascade-delete.svg?style=flat-square)](https://scrutinizer-ci.com/g/cesargb/laravel-cascade-delete)
[![Total Downloads](https://img.shields.io/packagist/dt/cesargb/laravel-cascade-delete.svg?style=flat-square)](https://packagist.org/packages/cesargb/laravel-cascade-delete)

# Cascading eliminations implemented in polymorphic relationships for the Laravel apps

This package permit add a trait for use in Elocuents Models that deletes in
cascade the Polymorphic Relations (`MorphMany` or `MorphToMany`).
cascade the Polymorphic Relations (`MorphOne`, `MorphMany` or `MorphToMany`).

## Instalation
## Installation

This package can be used in Laravel 5.5 or higher.

@@ -28,14 +31,26 @@ Use the trait `Cesargb\Database\Support\CascadeDelete` in your Elocuent Model an
namespace App;

use App\Tag;
use App\Image;
use App\Option;
use Illuminate\Database\Eloquent\Model;
use Cesargb\Database\Support\CascadeDelete;

class Video extends Model
{
use CascadeDelete;

protected $cascadeDeleteMorph = ['tags'];
protected $cascadeDeleteMorph = ['image', 'tags', 'options'];

public function image()
{
return $this->morphOne(Image::class, 'imageable');
}

public function options()
{
return $this->morphMany(Option::class, 'optionable');
}

public function tags()
{
@@ -44,8 +59,34 @@ class Video extends Model
}
```

Now you can delete an `App\Video` record, and any associated `App\Tag` records
will be deleted.
Now you can delete an `App\Video` record, and any associated `App\Image`, `App\Options` and pivot records for
`App\Tag` will be deleted.

## Delete Residuals

If you bulk delete a model with morphological relationships, you will have
residual data that has not been deleted.

To clean this waste you have the method `deleteMorphResidual`

Sample:

```php
Video::query()->delete();

$video = new Video;

$video->deleteMorphResidual();
```

### Command to remove all residuals

You can use Artisan command `morph:clean` to remove all residuals data from all
your Moldes that used the `Cesargb\Database\Support\CascadeDelete` trait.

```php
php artisan morph:clean
```

## Contributing

33 changes: 25 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -3,21 +3,27 @@
"description": "Cascading eliminations implemented in polymorphic relationships for the Laravel apps",
"keywords": [
"laravel",
"elocuent",
"eloquent",
"cascade",
"delete",
"polymorphic",
"relation"
"relation",
"residual"
],
"type": "library",
"require": {
"php": "^7.0",
"illuminate/database": "~5.5.0|~5.6.0",
"illuminate/events": "~5.5.0|~5.6.0"
"php": "^8.1",
"illuminate/console": "^10.0|^11.0|^12.0",
"illuminate/database": "^10.0|^11.0|^12.0",
"illuminate/events": "^10.0|^11.0|^12.0",
"illuminate/support": "^10.0|^11.0|^12.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0|^7.0",
"orchestra/testbench": "~3.5.0|~3.6.0"
"phpunit/phpunit": "^9.0|^10.5|^11.5.3",
"orchestra/testbench": "^8.0|^9.0|^10.0",
"laravel/legacy-factories": "^1.0.4",
"friendsofphp/php-cs-fixer": "^3.13",
"larastan/larastan": "^2.0|^3.0"
},
"autoload": {
"psr-4": {
@@ -29,12 +35,23 @@
"Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit --colors=always --coverage-text"
},
"license": "MIT",
"authors": [
{
"name": "Cesar Garcia",
"email": "cesargb@gmail.com"
}
],
"minimum-stability": "stable"
"extra": {
"laravel": {
"providers": [
"Cesargb\\Database\\Support\\CascadeDeleteServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
7 changes: 7 additions & 0 deletions config/morph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'models_paths' => [
app_path(),
],
];
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- vendor/larastan/larastan/extension.neon

parameters:
paths:
- src
- tests
level: 5
Loading