• Stars
    star
    703
  • Rank 64,186 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cascade Delete & Restore when using Laravel SoftDeletes

Header

Build Status Codacy Badge Codacy Badge StyleCI Badge

Laravel/Lumen Soft Cascade Delete & Restore

Cascade delete and restore when using the Laravel or Lumen SoftDeletes feature.

Why do I need it?

To make soft deleting and restoring relations easy.

If you enjoy features like MySQL cascade deleting but want to use Laravels SoftDeletes feature you'll need to do some extra steps to ensure your relations are properly deleted or restored.

This package is intended to replace those steps with a simple array that defines the relations you want to cascade.

Installation

Install with composer

composer require askedio/laravel-soft-cascade

From Laravel 5.5 onwards, it's possible to take advantage of auto-discovery of the service provider. For Laravel versions before 5.5, you must register the service provider in your config/app.php

Askedio\SoftCascade\Providers\GenericServiceProvider::class,

Lumen does not support the auto-discovery feature, you should manually add the provider.

Askedio\SoftCascade\Providers\LumenServiceProvider::class,

Usage

In your Model enable the trait and define $softCascade. Example.

use \Askedio\SoftCascade\Traits\SoftCascadeTrait;

protected $softCascade = ['profiles'];

For restricted relation use. Example.

use \Askedio\SoftCascade\Traits\SoftCascadeTrait;

protected $softCascade = ['addresses@restrict'];

$softCascade is an array of your relation names, in the example you'll see we've defined function profiles() for the relation.

Nested relations work by defining $softCascade in the related Model as you can see here.

After you've defined your relations you can simply trigger delete() or restore() on your Model and your relations will have the same task performed.

User::first()->delete();
User::withTrashed()->first()->restore();

It can also be used with query builder in this way because query builder listener is executed after query, we need to use transaction for rollback query on error due to restricted relationships

try {
    DB::beginTransaction(); //Start db transaction for rollback query when error
    User::limit(2)->delete();
	User::withTrashed()->limit(2)->restore();
    DB::commit(); //Commit the query
} catch (\Exception $e) {
    DB::rollBack(); //Rollback the query
    //Optional, if we need to continue execution only rollback transaction and save message on variable
    throw new \Askedio\SoftCascade\Exceptions\SoftCascadeLogicException($e->getMessage()); 
}

Supported Databases

  • MySQL
  • PostgreSQL
  • SQLite
  • SQL Server

Testing

I have written some very basic tests, certainly more needs to be done here. If you find this useful please help by testing other databases or writing better unit tests because I must move on.

Issues & Contributing

I will be using this with MySQL in a new API so any issues I find related to my use will be resolved. If you find an issue with MySQL please report it and I will fix it.

If you are using another database and have issues please contribute by submitting a pull request. I do not have time to test this with other database but assume all would work.

More Repositories

1

laravel-ratchet

A Ratchet Server built for Laravel
PHP
185
star
2

laravel-item-paginate

Paginate based on last item instead of pages.
PHP
66
star
3

laravel-profanity-filter

A string filter and validator for Laravel.
PHP
50
star
4

jquery-bootstrap-feedback

A jQuery, Bootstrap and html2canvas based Feedback/Bug Reporter
HTML
48
star
5

laravel-Cruddy

A really simple package that provides a CRUD JSON API for your Laravel 5 application..
PHP
12
star
6

Laravel5-Google-Calendar

A simple integration of Laravel 5 (or any PHP) and Google Calendar REST API
PHP
11
star
7

BillReminder

A simple Laravel and Google Calendar based bill reminder
PHP
9
star
8

Laravel-100-Page-Speed

A package to make it easier to hit a 100 Google Page Speed Insights Score, GTMetrix and Pingdom too.
PHP
8
star
9

Laravel5-RBAC

User Roles & Permissions for Laravel 5.2
PHP
7
star
10

jQuery-Cruddy

A CRUD plugin for jQuery & Bootstrap to easily generate tables, create, and edit dialogs
HTML
6
star
11

Laravel-Vendor-Package

A Laravel 5 Vendor Package Example
PHP
6
star
12

Laravel5-Bootstrap3-Starter-Site

A Modular Laravel 5.1 Bootstrap, jQuery, Fontawesome, based starter site.
PHP
6
star
13

serverless-ssm-seed

Serverless AWS SSM Parameter Store seed plugin
JavaScript
5
star
14

Laravelcp

A Laravel 5.1 Modular Control Panel
PHP
4
star
15

laravel-event-worker

A Laravel Event Worker
PHP
3
star
16

laravel-validator-filter

Filter items before your validate them with Laravel Validator.
PHP
3
star
17

bookshelf-simplepaginate

A simple bookshelfjs pagination based on Laravels simplePaginate.
JavaScript
3
star
18

pusher-lambda-promise

A basic Pusher.com trigger for AWS Lambda using promises.
JavaScript
3
star
19

Laravel5-Bootstrap-Extended-Template

An easily extendable Bootstrap 3 based Laravel 5.1 Template, based on SB Admin 2
PHP
1
star
20

Sinatra_Decent_Blog

Very simple Sinatra based blog.
HTML
1
star
21

l5cp-user

User management module for LaravelCP v2
PHP
1
star
22

l5cp-module

A basic example module for LaravelCP v2
PHP
1
star
23

Alexa-for-MacOS

An Electron based wrapper for alexa.amazon.com
JavaScript
1
star
24

CKEditor_4_Image_Upload

Simple hidden iframe image upload button for CKEditor 4
JavaScript
1
star