• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    PHP
  • Created over 9 years ago
  • Updated over 9 years ago

Reviews

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

Repository Details

Easily add an "undo" option to your Laravel application

Reanimate

Restoring models in laravel is easy. Simply call restore on the soft-deleted model and you're good to go. But what if you want to implement a simple undo mechanism in your application?

You need to take care of restoring the model, redirecting back, showing a success/error message...

Wouldn't it be nice if this process could be simplified?

Reanimate is a laravel package that allows you to do just that. It simplifies undoing of soft-deletes for your application.

Installation

Reanimate can be installed via composer, the details are on packagist, here.

To download and use this package, add the following to the require section of your projects composer.json file:

"mpociot/reanimate": "1.*",

Run composer update to download the package

php composer.phar update

That's it. Take a look at the implementation guide, to get started.

Docs

What Reanimate does

A simplified deletemethod on your controller might look like this:

public function delete( User $user )
{
	$user->delete();
	return Redirect::route( "userIndex" )->with( "message", "userDeleted" );
}

So how would you undo that delete call? You need to create a route, pass that object id, resolve the object, restore it if it exists and finally redirect back.

With Reanimate, this becomes:

public function delete( User $user )
{
	$user->delete();
	return Redirect::route( "userIndex" )->with( "message", "userDeleted" )->with( $this->undoFlash( $user ) );
}

undoFlash passes an array to your session with the following data:

"undo" => [
	"route"  => "userUndo",
	"params" => [ "DELETED_MODEL_ID" ],
	"lang"   => "user.undo.message"
]

So if your session has an undo array, you know that you should present your users a possibility to undo the deletion.

Just make sure that the undo route, points to the undoDelete method, available through the Reanimate Trait.

This method will then restore the model with the given ID and will redirect the user to a given index route.

Implementation & configuration

Every controller in your application, that currently takes care of deleting a model and now should benefit from Reanimate, needs to use the ReanimateModels Traits.

For example

namespace App\Http\Controllers;

class UserController extends Controller {
    use \Mpociot\Reanimate\ReanimateModels;
}

Deletes

When you redirect your users after a successful delete call, simply append the ->with( $this->undoFlash( $deletedModel ) ); data to your redirect.

undoFlash will automatically do two things for you:

Auto generate an undo route:

When no custom undo route is specified, Reanimate generates a named route by using the model name + "Undo".

Example model names and undo routes:

User -> userUndo

Category -> categoryUndo

FileEntry -> fileEntryUndo

Auto generate an undo language key:

By default, a lang key can be used within your views, for the undo action. This key is a lowercase representation of your model name + .undo.message

Example model names and lang keys:

User -> user.undo.message

Category -> category.undo.message

FileEntry -> fileentry.undo.message

Restores

The easiest way to restore your models, is to add a named route to your routes.php that matches the generated undo route name.

This route needs to point to the undoDelete method of your controller.

This method takes care of restoring the model with the given primary key and returns a redirect to an auto generated index route.

Auto generated model name:

Since this method only receives the ID of your model, but not the model itself, Reanimate tries to guess the correct model name for you.

It's done by using the singular version of your Controller's name.

Example controller names and the matching model names:

UserController -> User

CategoriesController -> Category

FileEntryController -> FileEntry

Auto generated index route:

When no custom index route is specified, Reanimate generates a named route by using the model name + "Index".

Example model names and undo routes:

User -> userIndex

Category -> categoryIndex

FileEntry -> fileEntryIndex

The redirect will also contain a flash data named message that contains either:

modelname.undo.restored on success or modelname.undo.invalid when no model with the given ID could be found.

Customization

Don't like the auto generated routes? Don't like the auto generated model names? No problem!

Simply override them in your controller class like this:

namespace App\Http\Controllers;

class UserController extends Controller {
    use \Mpociot\Reanimate\ReanimateModels;
    
    protected $undoDeleteModel = 'This\Is\My\Custom\Model';
    
    protected $indexRoute = 'home';
    
    protected $undoRoute = 'ohNooooo';
    
}

You can also write your own undoDelete method, if you want some more control:

namespace App\Http\Controllers;

class UserController extends Controller {
    use \Mpociot\Reanimate\ReanimateModels;
    
    public function myCustomUndoMethod( $primaryKey )
    {
    	return $this->restoreModel( $primaryKey , new User(), "myCustomIndexRoute" );
    }
    
}

Contributing

Contributions are encouraged and welcome; to keep things organised, all bugs and requests should be opened in the github issues tab for the main project, at mpociot/reanimate/issues

All pull requests should be made to the develop branch, so they can be tested before being merged into the master branch.

More Repositories

1

chatgpt-vscode

A VSCode extension that allows you to use ChatGPT
TypeScript
3,779
star
2

laravel-apidoc-generator

Laravel API Documentation Generator
PHP
3,386
star
3

whiteboard

Simply write beautiful API documentation.
JavaScript
1,224
star
4

teamwork

User to Team associations with invitation system for the Laravel 5 Framework
PHP
1,036
star
5

laravel-test-factory-helper

Generate Laravel test factories from your existing models
PHP
930
star
6

versionable

Laravel Model versioning made easy
PHP
687
star
7

laravel-testtools

Chrome extension to generate Laravel integration tests while using your app.
JavaScript
473
star
8

documentarian

Create beautiful API documentation
JavaScript
377
star
9

captainhook

Add Webhooks to your Laravel app, arrr
PHP
335
star
10

reauthenticate

Reauthenticate users by letting them re-enter their passwords for specific parts of your app.
PHP
296
star
11

laravel-firebase-sync

Synchronize your Eloquent models with a Firebase Realtime Database.
PHP
264
star
12

laravel-face-auth

Laravel Face authentication
PHP
157
star
13

TiSideMenu

iOS 7 / 8 style side menu with parallax effect. (Wrapper module for RESideMenu)
Objective-C
147
star
14

pipeline

Simple PHP pipelines
PHP
131
star
15

titanium-facebook-slide-menu

Example of how to implement a facebook like sliding menu in appcelerator's titanium mobile
JavaScript
117
star
16

blacksmith

The unofficial Laravel Forge PHP API
PHP
103
star
17

human-regex

Regular expressions for human beings, not machines
PHP
102
star
18

reflection-docblock

Fork of phpdocumentor/reflection-docblock to work with the api-documentation-generator
PHP
85
star
19

TiMotionEffects

UIMotionEffects for Appcelerator Titanium
Python
59
star
20

TiMWKProgressIndicator

A minimal progress indicator for iOS with status update support. Displays above UINavigationBar and below the Status Bar.
Objective-C
49
star
21

TiCircularSlider

Titanium circular slider module with many customizations.
Objective-C
47
star
22

TiCircularProgress

Circular progress indicator for Appcelerator Titanium
Objective-C
44
star
23

cockpit

Custom widgets for your Laravel app
PHP
38
star
24

titanium-social-modul

iOS6 module for facebook, twitter, sina weibo to use with appcelerator titanium
Python
38
star
25

laravel-slack-inviter

A little Laravel application to invite a user into your Slack team.
PHP
36
star
26

TiImageFromGIF

Create native imageviews from GIFs
Objective-C
35
star
27

TiCustomTab

Create Tabs with custom selected / unselected images.
Objective-C
34
star
28

laravel-composite-key

PHP trait to use composite keys in your Laravel Eloquent models.
PHP
34
star
29

codeception-testtools

Chrome extension to generate Codeception acceptance tests while using your app.
JavaScript
34
star
30

TiAndroidAutofocus

Prevents TextFields in Android to autofocus
Java
31
star
31

titanium-mobile-twitter-module

Basic twitter iOS 5 integration as a titanium module
Objective-C
30
star
32

TiAppirater

Titanium mobile iOS rating reminder
Objective-C
27
star
33

TiFaceRecognizer

CoreImage face detection module to use with the Appcelerator Titanium SDK
Python
27
star
34

titanium-mobile-alasset-module

Titanium module to get access to the iOS ALAsset framework
Objective-C
26
star
35

TiiMMAppSwitcher

iOS Titanium Mobile module - Replace iOS7 app switcher view with your own customized card view.
Objective-C
26
star
36

titanium-mobile-zip-module

zip module for appcelerators titanium mobile
C
25
star
37

chuck-norris-jokes

Create random Chuck Norris jokes.
PHP
25
star
38

TiScreenshotDetection

Detect when someone takes a screenshot in your Appcelerator Titanium iOS app.
Objective-C
20
star
39

TokenCompleteTextView

Java
20
star
40

lastfm-slack

Set your Last.fm tracks as your Slack status
PHP
19
star
41

TiAdvancedSplitWindow

Appcelerator titanium module to make use of the MGSplitView functions, not used by the Titanium SDK
Python
19
star
42

socialite-slack

Slack OAuth2 Provider for Laravel Socialite
PHP
18
star
43

TiTutorialView

Path like tutorial view as a commonJS module for Appcelerator Titanium
JavaScript
18
star
44

TiWebviewFragment

Simple helper class that is a workaround for Webviews Crashing / rendering blank on Android 4.4 in Titanium
Java
17
star
45

laravel-firebase-sync-example

Example project demonstrating the usage of the laravel-firebase-sync package
PHP
17
star
46

TiSplashScreen

Path like splash screen animation
Python
15
star
47

TiSDSegmentedControl

This is a titanium module for Olivier Poitrey's SDSegmentedControl library.
Objective-C
14
star
48

TiDocumentInteraction

Share/Print/Open files with the native activity view controller in your Titanium app
Python
12
star
49

mpociot

10
star
50

TiSMPageControl

Titanium mobile iOS module for some funky ScrollableView customization
Objective-C
7
star
51

llamero

A GUI application to easily try out Facebook's LLaMA models.
JavaScript
6
star
52

botman-spark-demo

BotMan + Cisco Spark demo project
PHP
5
star
53

psd2tss

JavaScript
5
star
54

feature-switch

A feature switcher for PHP
PHP
5
star
55

MagicPie

A Titanium implementation of https://github.com/Sk0rpion/MagicPie
Python
4
star
56

ti-crittercism-hook

A CLI hook for Appcelerator Titanium to automatically upload dSYM files to crittercism.
JavaScript
4
star
57

skeleton-php

PHP
3
star
58

TiIOS8WebviewFix

Prevent iOS8 iPad apps with WebViews from crashing
Python
3
star
59

laracon-eu-2017

My Laracon EU 2017 talk
PHP
3
star
60

php-uk-conference-2018

My PHP UK Conference 2018 Talk
PHP
3
star
61

titanium-twitter-oauth

A Twitter OAuth Module for Titanium Mobile
JavaScript
3
star
62

php-serbia-2018

The talk I gave at PHP Serbia 2018
PHP
2
star
63

ti-taffy

taffyDB wrapper for titanium
JavaScript
2
star
64

botman-workshop

PHP
2
star
65

TiShowcaseView

Native android module for Appcelerator Titanium that tries to wrap the "ShowcaseView" library.
2
star