Why soft deleted entities appear in query results?

前端 未结 6 760
独厮守ぢ
独厮守ぢ 2020-12-30 20:06

I am trying to implement soft deleting concept.

Here is my object:

class Post extends Eloquent {

    /**
     * The database table used by the model         


        
6条回答
  •  旧巷少年郎
    2020-12-30 20:30

    Sometimes, you will get the soft deleted table entries with get() even with eloquent and protected $softDelete = true;.

    So to avoid this problem, use

    ...->whereNull('deleted_at')->get();
    

    For example, this query will fetch all rows including soft deleted.

    DB::table('pages')->select('id','title', 'slug')
                                       ->where('is_navigation','=','yes')
                                       ->where('parent_id','=',$parent_id)
                                       ->orderBy('page_order')
                                       ->get();
    

    So the proper method is,

    DB::table('pages')->select('id','title', 'slug')
                                       ->where('is_navigation','=','yes')
                                       ->where('parent_id','=',$parent_id)
                                       ->whereNull('deleted_at')
                                       ->orderBy('page_order')
                                       ->get();
    

提交回复
热议问题