CodexBloom - Programming Q&A Platform

How to implement guide with laravel 9 eloquent soft deletes optimization guide as expected

👀 Views: 57 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-02
laravel eloquent soft-deletes php

I'm writing unit tests and Quick question that's been bugging me - I've been struggling with this for a few days now and could really use some help... I'm working with an scenario with soft deletes in Laravel 9 where the `deleted_at` timestamp is not being set when I call the `delete()` method on an Eloquent model. I have the `SoftDeletes` trait included in my model, and I expected the record to be marked as deleted instead of being permanently removed from the database. Here's the relevant part of my model: ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use SoftDeletes; protected $dates = ['deleted_at']; } ``` In my controller, I'm attempting to delete a post like this: ```php public function destroy($id) { $post = Post::findOrFail($id); $post->delete(); return redirect()->route('posts.index'); } ``` However, when I check the database afterward, the post is completely removed instead of being soft-deleted. I have also verified that the `deleted_at` column exists in my database migration: ```php public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->softDeletes(); $table->timestamps(); }); } ``` I've tried clearing the cache with `php artisan config:cache` and `php artisan cache:clear`, but the scenario continues. Additionally, I checked for any global scopes that might be interfering, but I haven't found any. Could someone guide to understand why the soft delete isn't functioning as intended? Is there something I've missed or misconfigured? For context: I'm using Php on macOS. The project is a microservice built with Php. Any suggestions would be helpful. This is happening in both development and production on CentOS. Thanks, I really appreciate it!