'return table except deleted_at (softdeletes) laravel

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!



Solution 1:[1]

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

Solution 2:[2]

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']);
}

Solution 3:[3]

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Humberto Barbosa
Solution 2 Sok Chanty
Solution 3 Supreme Dolphin