To do this we need to open Model file in code editor, where you want to add soft delete.
add this line in top of the model file.
use Illuminate\Database\Eloquent\SoftDeletes;
Now we make or model to use Soft delete.
class Product extends Model { use SoftDeletes; protected $dates = ['deleted_at']; }
We need to create a migration file so we can add "delete_at" column in database table.
so to do that, Navigate to the project folder using terminal and run command something like this
php artisan make:migration add_soft_delete_to_products_table
Newly created migration file should be in directory "yourProject/database/migrations". Open this file using code editor.
now add these 2 lines if already not there
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
Then
in functions add code like this
// up function should look like this public function up() { Schema::table('products', function(Blueprint $table) { $table->softDeletes(); }); }
And down function
public function down() { Schema::table('products', function(Blueprint $table) { $table->dropSoftDeletes(); }); }
by running
php artisan migrate
It will add the deleted_at field in database table. and then you can delete using delete() function in query. If will be soft delete.
//Now this will do a soft delete Product::where('id',$id)->delete();