Laravel Soft Delete Example Tutorial From Scratch

Today, We want to share with you Laravel Soft Delete Example Tutorial From Scratch.In this post we will show you Cascading Soft Deletes with Laravel 5, hear for Adding Soft Deletes to Existing Database Table in Laravel we will give you demo and example for implement.In this post, we will learn about How To Make Unique Validations Work With Soft Delete In Laravel with an example.

Laravel Soft Delete Example Tutorial From Scratch

There are the Following The simple About Laravel Soft Delete Example Tutorial From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel soft delete relationship, so the some major laravel soft delete migration for this example is following below.

  • soft delete in Laravel
  • Laravel drop softDeletes

Example 1 : soft delete in Laravel

Migration Example:

first of all you create a Database new Migration Like this..

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function(Blueprint $table) {
            $table->increments('id');
            $table->string('product_name');
            $table->text('product_description');
            $table->softDeletes();
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop("products");
    }
}

Products Model

and then Laravel Simple Add this below line protected $dates = [‘deleted_at’];.as well as included use SoftDeletes;

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
    use SoftDeletes;
    public $fillable = ['product_name','product_description'];
    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}

app/Http/Controllers/ProductController.php

simple add this line On destroy Methods to laravel create a collum Like deleted_at to soft delete.

public function destroy($id)
{
    $products = \App\Products::find($id)->delete();
    return redirect('products')->with('success','Product has been deleted');
}

Example 2 : Laravel drop softDeletes from a table in a migration

On your Laravel migration class:

Laravel migration table down methods to simple softDeletes

public function down()
{
    Schema::table("members", function ($table) {
        $table->dropSoftDeletes();
    });
}

Illuminate\Database\Schema\Blueprint.php:

public function dropSoftDeletes()
{
    $this->dropColumn('deleted_at');
}
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Soft Delete Example Tutorial From Scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment