Why soft deleted entities appear in query results?

0 votes

I am trying to implement soft deleting concept.

Here is my object:

class Post extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';
    protected $softDelete = true;

    ...

Soft delete is on.

Now, if I 'delete' a post, it gets a 'deleted_at' timestamp:

Apr 3 in Laravel by kartik
• 10,990 points
16 views

1 answer to this question.

0 votes

Hello @kartik,

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();

Hope this works!

Thank you!!

answered Apr 3 by Niroj
• 22,110 points

Related Questions In Laravel

0 votes
1 answer

What is Laravel framework? Why one should use Laravel?

Laravel is a PHP web-framework; it utilized ...READ MORE

answered Mar 17 in Laravel by Niroj
• 22,110 points
67 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17 in Laravel by Niroj
• 22,110 points
32 views
0 votes
1 answer

What are the important directories used in a common Laravel application

Hey @Kartik. Directories used in a common Laravel ...READ MORE

answered Mar 17 in Laravel by Niroj
• 22,110 points
33 views
0 votes
1 answer

What is reverse routing in Laravel?

 Laravel reverse routing is generating URL's based ...READ MORE

answered Mar 17 in Laravel by Niraj
293 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 22,110 points
417 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 22,110 points
62 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 22,110 points
64 views
0 votes
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 22,110 points
139 views
0 votes
1 answer

What are the query builder in Laravel?

Hey, Laravel's database query builder provides a convenient, ...READ MORE

answered Mar 19 in Laravel by Niroj
• 22,110 points
35 views
0 votes
1 answer

How to enable query log in Laravel?

Hello, You can use simple function that is ...READ MORE

answered Mar 23 in Laravel by Niroj
• 22,110 points
29 views