Here, i will give you very simple example of how to use laravel 6 model eloquent soft delete. you can use soft delete with laravel 6.
You need to follow bellow tutorial for implement soft delete with laravel 6 model.
i will explain you step by step implementation of soft delete in laravel application. we have to use soft delete for safety and backup in laravel.
How work soft delete, laravel add deleted_at
So, how to use in our project, so first when you create table moigration then you have to add softDeletes(). you can see like bellow example of migration.
Migration Example:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function(Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop("items");
}
}
Here Add 10
Ok, now you can find deleted_at column in your items table and you have also model should look like as bellow:
Items Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Item extends Model
{
use SoftDeletes;
public $fillable = ['title','description'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
}
Now, you can use Item model as normally like you use before, you get all record like this way.
$data = Item::get();
It will return all record that have deleted_at = null only and you can also remove record like this way:
$data = Item::find(1)->delete();
You can also get deleted records with soft delete.
$data = Item::withTrashed()->get();
You can check and use it's really good....
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?