All About Cascading Soft Deletes: Laravel 5


Before the starting Cascading Soft Deletes in Laravel. Let’s see what is Laravel why we use web application framework. Laravel is Php framework with expressive, elegant syntax. The aims of the Laravel are to make the development process a pleasing one for the developer without the sacrificing application functionality. Brilliant and happy developer make the best code. Additionally, frameworks implemented in other languages such as Ruby on Rails, ASP.NET MVC, and Sinatra.

Here, the question arises why we use Laravel? Because accessible, yet powerful, providing powerful tools needed for large, robust applications. The best inversion of control container, expressive migration system, and tightly integrated unit testing support give you the tools you need to build any application.

Laravel comes with the new feature with the form of the package and every Laravel development company is working on it. Package of the Soft Cascading. With the use of this Laravel make simple, secure and easy to implement cascade deletes and restore on related modules using the soft deleting. 



Soft Deleting 

Soft Deleting purpose of using the removing records from your database, Eloquent can also “ soft delete” modules. If the models are soft deleted, they are not actually removed from your database. In place, a deleted_at attribute is set on the models and inserted into the database. In case of the model has a non-null  deleted_at value, the model has been soft deleted. To use soft deletes for a model, use the Illuminate\Database\Eloquent\SoftDeletes  trait on the model and add the deleted_at column to your $dates property:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Flight extends Model
{
    use SoftDeletes;
    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}

In between, you should add the deleted_at column to your database table. Schema builder contains a helper method to create this column:

Schema::table('flights', function ($table) {
    $table->softDeletes();
});

At the time when you call the deleted method on the model, the deleted_at column will be set the current date and time. When querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all the query results. 

To create if a given model instance has been soft deleted, use the trashed method:

if ($flight->trashed()) {
   //
}

Laravel Soft Deleting also comes with Restoring deleted records. Restoring records is part of the Querying Soft Deleted Models. So let’s see in the Querying Soft Deleted Models.

Including Soft Deleted Models

As I earlier noted above that soft deleted models will automatically be excluded from the query results. Wherever you may force soft deleted models to appear in a resultset using the withTrashed method on the query:

$flights = App\Flight::withTrashed()
                ->where('account_id', 1)
                ->get();

Method withTrashed is  you can also be used on a relationship query:

$flight->history()->withTrashed()->get();

Retrieving Only Soft Deleted Models

 Method onlyTrashed will retrieve only soft deleted models:

$flights = App\Flight::onlyTrashed()
                ->where('airline_id', 1)
                ->get();


Restoring Soft Deleted Models

Restore the data means that It is possible that sometime you may wish to “un-delete” a soft deleted model. So, restore a soft deleted models into an active state, use the restore method on a model instance:

  $flight->restore();

You can also use the restore method at the phase of the query to get quickly restore multiple models. Other  “mass” operations, that will not fire any model events for the models that are restored: 

App\Flight::withTrashed()
        ->where('airline_id', 1)
        ->restore();

Like the withTrashed method, the restore method may also be used on relationships:
$flight->history()->restore();

Permanently Deleting Models

If you really need to remove a model from your database. To remove permanently remove a soft deleted model from the database, use the forceDelete method:

// Force deleting a single model instance...
$flight->forceDelete();

// Force deleting all related models...
$flight->history()->forceDelete();


Traditionally Deleting Relations

Traditionally everyone used MySQL foreign key constraints to cascade delete related the records. Laravel create or say make it easy to use foreign keys in migrations. And set ‘onDelete’ to cascade and walla, your relations will be deleted automatically.

Here the question arises that what happens when you enable softDeletes? That Database never gives a sign or told that actually ‘delete’ a record, in the place of ‘deleted_at’ field is updated. So with your cascading deletes, nothing happens and related records are left alone.

Conclusion

Laravel brings new feature a day before the new feature launch Cascading Soft Deletes. Earlier use the traditional method to delete the records, Now, every organization use this delete method to improve their codes. Best package for the Laravel is  Cascading Soft Delete.visit here : http://www.elsner.com/services/laravel-development/


Author bio:

Author -Harshal Shah


Address-305,306 Iscon Center,
Shivranjani Cross Road, Satellite,
Ahmedabad, India.

Phone number- +91 79 4006 2525

Popular posts from this blog

How Beacon Apps Will Impact the Digital Commerce

Top Free Magento Themes for Magento Developers

7 Excellent Benefits of Magento for E-commerce Website Development