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, 2020 in Laravel by kartik
• 37,520 points
5,082 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, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How to enable query log in Laravel?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,525 views
0 votes
1 answer

How to Make Laravel Eloquent “IN” Query?

Hello @kartik, Here is how you do in ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,840 points
1,019 views
0 votes
1 answer

How can I build a condition based query in Laravel?

Hello @kartik, Try this: $query = DB::table('node'); if ($published == ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,840 points
672 views
0 votes
1 answer

Why to use DB::raw inside DB::select in Laravel?

Hello @kartik, DB::raw() is used to make arbitrary SQL ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,840 points
4,221 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
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, 2020 in Laravel by Niroj
• 82,840 points
44,691 views
0 votes
1 answer

How to chunk results from a custom query in Laravel

Hello @kartik, Try something like this: <?php $max = 100; $total ...READ MORE

answered Dec 7, 2020 in Laravel by Niroj
• 82,840 points
2,280 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, 2020 in Laravel by Niroj
• 82,840 points
1,155 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP