Showing posts with label laravel soft delete. Show all posts
Showing posts with label laravel soft delete. Show all posts

Laravel Soft Delete Example Tutorial

Laravel Soft Delete Example Tutorial

Hi Guys,

Today , I will learn how to utilize soft efface in laravel,we are tell simple example of laravel soft expunge.it will laravel soft efface migration.we abstract row from database when efface record from site. But laravel introduce SoftDeletes in models that way we can't abstract from database but if abstract record from front side then it doesn't show record on front. So we can retrieve record from database if we abstract erroneous row.

How work soft efface, laravel integrate expunged_at column on the table that be default will be null and when we abstract then it will place current timestamp, Laravel Model always fetch that record have only expunged_at = null.

So, how to utilize in our project, so first when you engender table moigration then you have to integrate softDeletes(). you can optically discern like bellow example of migration.

Here i bellow example you can learn soft efface in laravel

Create Post Migration Soft Delete

In this post migration open Post migration file and put the below same code.

Path: /database/migrations/2020_01_02_095534_create_posts_table.php
<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('body');
            $table->softDeletes();
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}
Create Post Model Soft Delete

Next, go to app/Post.php and open post model file and put the below same code.

Now you can find deleted_at column in your post table and you have also model should look like as bellow

here following path of model fille

Path:/app/Post.php
<?php
namespace App;

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

class Post extends Model
{
    use SoftDeletes;

    protected $fillable = ['title','body'];

    /**
    * The attributes that should be mutated to dates.
    *
    * @var array
    */
    protected $dates = ['deleted_at'];
}
Get All Records

Now, you can use Post model as normally like you use before, you get all record like this way.

$data = Post::get();
Delete one Record

It will return all record that have deleted_at = null only and you can also remove record like this way:

$data = Post::find(1)->delete();
Get All Records After Delete one Record

Now, you can use Post model as normally like you use before, you get all record After Delete one Record like this way.

$data = Post::withTrashed()->get();

You can check the table records.

It will help you..