Search code examples
phplaravellaravel-query-builder

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?


Solution

  • 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