Skip to content
Advertisement

find() does not get the existing row

I have a resource Controller with this index method like this:

JavaScript

But it returns this error:

Trying to get property ‘slug’ of non-object

And when I dd(Category::find(25), Category::find(24), Category::find(23), Category::find(22)); I get NULL results.

Meaning that it can not find data with specified ids.

However there are 25 records stored at the categories table:

enter image description here

So what is going wrong here?

UPDATE #1:

Category.php Model:

JavaScript

UPDATE #2:

Result of dd(Category::where('cat_id', 25), Category::where('cat_id', 24), Category::where('cat_id', 23), Category::where('cat_id', 22));:

enter image description here

UPDATE #3:

Result of dd(Category::where('cat_id', 25)->get(), Category::where('cat_id', 24)->get(), Category::where('cat_id', 23)->get(), Category::where('cat_id', 22)->get());

enter image description here

Advertisement

Answer

If you use SoftDeletes trait then laravel just update deleted_at column. The column isn’t actually deleted from database, it just marked as delete. To find columns which is softly deleted, use Model::withTrashed()->find().

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement