return table except deleted_at (softdeletes) laravel

Viewed 840

I'm trying to return a table without deleted rows (softdeletes)

This is my code

public function getMailRecipients($meoId){
    return DB::table('mail_recipients')->where('meo_id', '=', $meoId)->select('name', 'email')->get();
}

but I get all rows, even those removed through softdeletes, What else should I add to avoid that?

softdeleted rows

softdeleted rows thanks!

3 Answers

Note *: The soft deleting feature works when using Eloquent. If you are querying the results with query builder you will eventually see all the records trashed and not trashed.

You can try with this:

public function getMailRecipients($meoId)
{
    return DB::table('mail_recipients')
          ->whereNull('deleted_at')
          ->where('meo_id', $meoId)
          ->get(['name', 'email']);
}

you are using query builder (facade DB) in this case you should do this:

DB::table('mail_recipients')->where('meo_id', '=', $ meoId)->whereNull('deleted_at')->select('name', 'email')->get();

If you use the model, you must use the SoftDeletes trait

class Flight extends Model{
 use SoftDeletes;
}

see more in the documentation https://laravel.com/docs/8.x/eloquent#soft-deleting

If you arrived here because you are using protected $softDelete = true in your model and Laravel seems to ignore it, the solution is to use Illuminate\Database\Eloquent\SoftDeletes in your model. Works everytime!

Related