E
E
Evgeny Romashkan2018-05-01 14:32:25
Laravel
Evgeny Romashkan, 2018-05-01 14:32:25

Is it normal for a Model::all() query to return Soft deleted models as well?

The documentation says that Soft Deleted models are automatically excluded from any selections.

spoiler
As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to appear in a result set using the withTrashed method on the query:
$flights = App\Flight::withTrashed()
->where('account_id', 1)
->get();
https://laravel.com/docs/5.6/eloquent#deleting-models

However, the Model::all() and Model::get() methods return all models to me, including the remote ones. Is this normal behavior? And how then to select all models except remote ones?
Here is my entire code:
public function index()
    {
        return Category::all();
    }

Model code:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    public static function boot() {
        static::deleting(function(Category $category) {
            //$subcategories = $category->subcategories;
            //foreach ($subcategories as $subcategory) $subcategory->delete();
        });
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2018-05-01
@EvgeniiR

By asking a lot of questions, I managed to see the code that the author wrote. From the code, it became clear that he overridden the method in the model boot, but forgot to call the parent method boot.
In general, redefining boot for model events is bad practice. Read the documentation for good practice.

A
Alexander Aksentiev, 2018-05-01
@Sanasol

So you are doing something very wrong.
The code from the documentation says nothing about your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question