Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Surajshetty's avatar

Call to undefined method Illuminate\Database\Eloquent\Builder::withoutTrashed()

Getting error after including this --soft-deletes php artisan make:filament-resource Department --generate --view --soft-deletes Need Help

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like you're trying to use the withoutTrashed method on an Eloquent query builder instance, but you're encountering an error because the method is undefined. This method is provided by the SoftDeletes trait, which you need to use in your Eloquent model to enable soft deleting functionality.

Here's how you can resolve the issue:

  1. Ensure that you have used the SoftDeletes trait in your Department model. Your model should look something like this:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Department extends Model
{
    use SoftDeletes;

    // Your model's code...
}
  1. After adding the SoftDeletes trait, you need to update your database table to include a deleted_at column if you haven't already. You can create a new migration to add this column:
php artisan make:migration add_deleted_at_to_departments_table --table=departments

Then, in the generated migration file, add the deleted_at column:

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

class AddDeletedAtToDepartmentsTable extends Migration
{
    public function up()
    {
        Schema::table('departments', function (Blueprint $table) {
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::table('departments', function (Blueprint $table) {
            $table->dropSoftDeletes();
        });
    }
}
  1. Run the migration to update the database:
php artisan migrate

After you've done these steps, you should be able to use the withoutTrashed method on your Department model without any issues. This method will ensure that your query only includes models that have not been soft-deleted.

Here's an example of how you might use it in a controller:

use App\Models\Department;

public function index()
{
    $departments = Department::withoutTrashed()->get();
    return view('departments.index', compact('departments'));
}

This will retrieve all departments that have not been soft-deleted. If you want to retrieve only the soft-deleted models, you can use the onlyTrashed method. If you want to retrieve all models, including those that have been soft-deleted, you can use the withTrashed method.

4 likes

Please or to participate in this conversation.