user3718908x100
user3718908x100

Reputation: 8509

Eloquent not returning records when using SoftDeletes

I'm having a weird issue, I uploaded my application to my server for live testing. I am able to save data but nothing is returned when I try to get all rows using Eloquent. However on my localhost it works just fine.

I have tried:

$xtras = Extra::with('category')->paginate(10);

and

$xtras = Extra::with('category')->get();

and

$xtras = Extra::all();

But none of the above worked, when I returned $xtras it was always an empty array.

So then I decided to try:

$xtras = DB::table('extras')->get();

And that seems to work just fine, it returns the data. I have checked my model and nothing seems missing because like I said it works on my localhost just not on my server.

This is my Extra class:

<?php

namespace App\Models;

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

class Extra extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $appends = ['checked'];


    public function category() {
        return $this->belongsTo('App\Models\ExtraCategory');
    }

    public function getCheckedAttribute() {
        return false;
    }
}

Upvotes: 1

Views: 2179

Answers (1)

patricus
patricus

Reputation: 62278

When using SoftDeletes, any value in your deleted_at column other than NULL will tell Laravel that that record is "deleted", so it won't show up in normal Eloquent queries, unless you use the withTrashed() scope.

Take a look at how you're saving/inserting the data and make sure that the deleted_at column is being set to NULL for records that are not supposed to be deleted.

Upvotes: 4

Related Questions