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.

Read Also:  Laravel Delete Queued Jobs

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

Read Also:  Laravel Aggregate Object and Debugging eloquent methods

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:

Read Also:  Laravel Eloquent Useful Tips and Tricks

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Create RESTful API using PHP
  2. Read Also:  Woocommerce Get product category name in array
  3. Read Also:  Laravel 6 Deletes method using Eloquent ORM
  4. Read Also:  PHP Laravel Soft Delete Migration cascade
  5. Read Also:  Query Scope using Laravel 6 Eloquent Example
  6. Read Also:  Ajax Upload Multiple Images without form submitting
  7. Read Also:  Laravel 6 Delete and soft delete Examples
  8. Read Also:  Eloquent ORM Soft Delete in Laravel 6
  9. Read Also:  Soft Delete Cascade using Laravel Eloquent Example
  10. Read Also:  Create WordPress Custom Admin Page