2

In my table i don't have deleted_at column but the Laravel query always checks if deleted_at is null, resulting in this error message:

Column not found: 1054 Unknown column 'orders.deleted_at' in 'where clause'

Below is my code

public function load($id) {
    return $this
        ->select(sprintf('%s.*', $this->getTable()))
        ->where(sprintf('%s.%s', $this->getTable(), $this->getKeyName()), '=', $id)
        ->first();
}

How to fix this?

miken32
  • 42,008
  • 16
  • 111
  • 154

3 Answers3

21

You have to remove the traits use SoftDeletes; from your model file.

and then use below code:

 return $this->where(sprintf('%s.id', $this->getTable(), $this->getKeyName()), '=', $id)
        ->pluck('id');'
Madhuri Patel
  • 1,270
  • 12
  • 24
-1

As you are using SoftDeletes, so it has to save the date and time when the record is deleted.

Simply add this into your up function in table migration:

$table->softDeletes($column = 'deleted_at', $precision = 0);

More about SoftDeletes: https://laravel.com/docs/8.x/eloquent#soft-deleting

Akif Malik
  • 49
  • 1
  • 1
  • 6
-2

Try adding withTrashed() to your chain:

public function load($id) {
    return $this
        ->withTrashed()
        ->select(sprintf('%s.*', $this->getTable()))
        ->where(sprintf('%s.id', $this->getTable(), $this->getKeyName()), '=', $id)
        ->first();
}
PhillipMcCubbin
  • 495
  • 1
  • 4
  • 10
  • The database table was not configured for soft deleted models, so the model should not import the soft delete trait. That was the solution years ago, and it remains so now. – miken32 Aug 09 '22 at 00:17